Hey all, first time on here and I've got a question. I am doing an assignment where I have to make a program that has to find the average of any amount of entered integers, but using pointers only. I think I may have a solution, but when I go to debug it states that one of my variables hasn't been initialized. I'm not quite sure what they're meaning by that, but if anyone could provide some insight it would be greatly appreciated.
Here's what I have so far:
#include <iostream>
using namespace std;
void showNumbers(int[], int*);
int main()
{
int *values;
int *total = 0;
int *average;
int *numberOfValues;
int *count;
*numberOfValues = *count;
//makes array large enough to hold all inputed values
values = new int [*numberOfValues];
//asks user to input values
cout <<"Enter a series of positive numbers:"<<endl;
cout << "Entering a negative number will stop input"<<endl;
for (*count = 0; *count++;)
{
do
{
cin >> values[*count];
}
while (values [*numberOfValues] > 0);
}
//finds sum of values
for (*count = 0; *count++;)
{
*total += values[*numberOfValues];
}
//finds the average
*average = *total / *numberOfValues;
//shows results
cout << "The numbers you entered were follows:"<<endl;
showNumbers (values, numberOfValues);
cout << "The average is: "<< *average << endl;
//include files
//function declarations
int main() {
//declare variables
int values = 0; //put description here of what the variable means
int total = 0; //put description here of what the variable means
int average = 0; //put description here of what the variable means
constint numberOfValues = 10; //put description here of what the variable means
int count = 0; //put description here of what the variable means
int ValueArray[numberOfValues]; //an array of ints
//call functions with a pointer as the argument
// and / or have the function return a pointer
} //end of main
//function definitions
So what I am saying is you can still have "normal" variables like ints but you use pointers with functions.
There is absolutely no need for counter to be a pointer, because it's only used to do counting in a loop.
the normal form for for loops is this:
1 2 3 4
for (count = 0; count < 10; count++) {
//do this code 10 times
}
While loops are like this:
while (test-expression) {
//do this code until test-expression is false
}
so this won't work at all:
while (values [*numberOfValues] > 0);
with an array you need a for loop to go through it one item at a time, so this won't work at all either:
*total += values[*numberOfValues];
the usual form is this:
1 2 3 4
for (count = 0; count < numberOfValues; count++) {
ValueArray[numberOfValues] = count; //puts 0 1 2 3 etc into the array upto numberOfValues-1
}
I think this is a poor example of an assignment about pointers, because students need to be better at doing code without pointers first.