- 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.
I am getting all 0s output. So obviously I did something wrong here.
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: