• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

c++ array pointer question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

ssjwizard

Has slightly less legible writing than Thideras
Joined
Mar 12, 2002
Ok so I am trying to write a program that uses a pointer from a function to fill an array in main.

Code:
#ifndef functions_h
#define functions_h
void fillArray(int*,int);
void avgArray(int*,int,float*);
void stDev(int*,int,float,float*);
#endif
Code:
#include <iostream>
#include <cmath>
#include "functions.h"
using namespace std;

void fillArray(int *point,int length){
	srand(123);
	for (int i=0;i>length;i++) {
	*point=rand()%2000;
	point++;
	}
}
void avgArray(int *point,int length,float *average) {
	int sum=0;
	for (int i=0;i>length;i++){
	sum+=*point;
	point++;
	}
	*average=sum/length;
}
void stDev(int*point,int length,float avg,float*std){
	int sum=0;
	for (int i=0;i>length;i++){
	sum+=pow((*point-avg),2);
	point++;
	}
	*std=(1/length*sum);
	*std=sqrt(*std);
}
Code:
int main() {
//Declare variables
int opt, len, nums[500],*point;
float avg,std;
bool runit=true;

while (runit) {
cout << "Please enter the desired number of values between 1 and 500: ";
cin >> len;
point=nums;
fillArray(point,len);
avgArray(point,len,&avg);
stDev(point,len,avg,&std);
cout << setprecision (4) << fixed;
cout << "The mean average of the " << len << " random numbers is : " << avg << endl;
cout << "The standard deviation is calculated to : " << std << endl;

cout << "Would you like to run the program again?\nEnter 1 to continue\nEnter 2 to exit\n";
cin >> opt;
if (opt!=1){runit=false;}
}
}

I am getting all 0s output. So obviously I did something wrong here.
 
Last edited:
At a glance: Your for loops won't execute unless you reverse the > sign.
Code:
	for (int i=0;i>length;i++) {
should instead be
Code:
	for (int i=0;i<=length;i++) {
 
Ok well I cant beleive I didnt see that to begin with. Anyways its still broken I get the mean but my standard deviation is coming back as 0 every time.
 
i think you need the void stDev to be a double.. though I really am too out of it to be of any use atm
 
Your equation for std should be square root of (sum/length), not square root of (1/length * sum).
 
got it all worked out. BTW the math is the same with either method(1/len * sum) or (sum/len) the second is shorter though.
Code:
int main() {
//Declare variables
int opt, len, nums[500],*point, seed;
float avg,std;
bool runit=true,valid=true;


cout << "Darcy Viohl Quiz 6 Standard Deviation\n\nThis program will take a quantity of numbers\nFill an array with random values\nIt will then calculate the mean and standard deviation" <<endl;
while (runit) {
do {
cout << "Please enter the desired number of values between 1 and 500: ";
cin >> len;
if (0<len && len<501){valid=false;}
} while (valid);
cout << "\nPlease enter a seed value: ";
cin >> seed;
point=nums;
fillArray(point,len,seed);
avgArray(point,len,&avg);
stDev(point,len,avg,&std);

cout << setprecision (4) << fixed;
cout << "\nThe mean average of the " << len << " random numbers is : " << avg << endl;
cout << "The standard deviation is calculated to : " << std << endl;

cout << "\nWould you like to run the program again?\nEnter 1 to continue\nEnter 2 to exit\n";
cin >> opt;
if (opt!=1){runit=false;}
}
}
Code:
void fillArray(int *point,int length,int seed){
	srand(seed);
	for (int i=0;i<length;i++) {
	*point=rand()%2001;
	point++;
	}
}
void avgArray(int *point,int length,float *average) {
	int sum=0;
	for (int i=0;i<length;i++){
	sum+=*point;
	point++;
	}
	*average=sum/length;
}
void stDev(int*point,int length,float avg,float*std){
	float sum=0,temp;
	for (int i=0;i<length;i++){
	sum+=pow((*point-avg),2);
	point++;
	}
	temp=(sum/length);
	*std=sqrt(temp);
}
 
Last edited:
Your problem illustrates why they usually tell don't mess with pointer arithmetic in class.

A pointer is just a memory address right? (Note the following example is dumbed down so don't complain if I'm "adding" the wrong direction and ignoring endienness).

Say for example the pointer point points to 0x00000001. (say that 10 times fast).

When you invoke point++, point becomes 0x00000002.

This is just the same as putting ++ on an int.

int i = 1; i++; //i == 2

When you call each function, you're not setting point back to the first index, it's remaining where you last left it in the last function you called. My guess is also that you haven't tried using 500 as an option because you'd end up going beyond the boundaries of the array and probably would crash.

To fix this, either use the index instead of ++ (i.e. point[]) or reset point to nums before each function call. A la:

Code:
point=nums;
fillArray(point,len,seed);
point=nums;
avgArray(point,len,&avg);
point=nums;
stDev(point,len,avg,&std);

When loops are involved you can also use pointer addition differently.

i.e.

Code:
void fillArray(int *point,int length,int seed){
	srand(seed);
	for (int i=0;i<length;i++) {
	*(point + i)=rand()%2001;
	}
}

Thats the same as saying point.

Hope this helps :).
 
Actually it works just fine I got it all solved last night. The reason I never reset the point variable is because once it gets sent to the function the point++ is modifying the local variable not the global from main. Therefore each function is sent the original address of nums[0].
I there was a bit of confusion on my head as the best way to do it(not a good day yesterday) and theres about 5 different ways to make that incremental call. Anyway thank you for your input while it wont serve me today having reassurance of additional methods will likely serve me in the future ;p
 
Last edited:
Ach ya know what, I got your problem mixed up with another bug I chased down where the pointers were being passed by reference (int*& point) and someone was doing pointer arithmetic on them. My bad : o ).
 
Thanks for the input though I'll be sure to watch for that mistake, should i ever need to do that.
 
Back