Program will not debug, pair of eyes needed

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;

//clear memory
delete [] values;
values = 0;
return 0;

}

// function to display all inputed numbers
void showNumbers (int array[], int *size)
{

for (int*count = 0; *count < *size; *count++)
{
cout << array[*count]<<" ";
cout << endl;
}
}
Hi elli med,

First please post your code in code tags - the <> button on the right under format. Makes it easier to read.

Ok you need to initialise your variables.

What they mean when they say write a program with pointers is this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//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
const int 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.

Hope this helps



Last edited on
the value which is not initialized is cout << array[*count]<<" ";
array doesn't exist in code above
Topic archived. No new replies allowed.