Help with array input and output

I need help with storing values in an array and outputting them in two different modules.

I'm able to input values, but it either isn't being stored in the array or something is wrong with the output.

This is my code:

void inputList(double values[], int &count)
{

for (count = 0; count <= 20 - 1;)
{
cout << "Enter a value: " << endl;
cin >> values[count]; // this should be storing the values into the array correct?

if (values[count] != -1)
{
count++;
}
else
{
break;
}
}
}

void printList(double values[], int n, int valuesPerLine)
{
int count = n;
for (count = 0; count <= n; count++)
{
cout << values[count];// this should be outputting the values stored in the array correct?
}
}

When I go to display the array I only get the first value I input instead of the whole array.
My output is:

Edwin's statistical calculator.

Please follow instruction carefully.
Enter a value at a time (at least 3 to up to 20).
You must enter valid data or the program will not work.
Enter -1 to signal end of data (-1 or -1.0).

Enter a value:
1
Enter a value:
2
Enter a value:
3
Enter a value:
4
Enter a value:
5
Enter a value:
-1
1 Press any key to continue . . .

Any help would be appreciated!
Hi,

Pleas always use code tags:

http://www.cplusplus.com/articles/z13hAqkS/


With this:
1
2
3
4
5
6
7
8
void printList(double values[], int n, int valuesPerLine)
{
int count = n;
for (count = 0; count <= n; count++)
{
cout << values[count];// this should be outputting the values stored in the array correct?
}
}


The correct form of a for loop is :

1
2
3
for (Counter = 0; Counter < n; Counter++) {

}


Your version goes past the end of the array. Arrays start at zero, the last element is size-1. A for loop ends when when the end condition expression becomes false.

In the inputList function you are using count in the for loop and as an argument to the function. Avoid doing that.

count exists in the std namespace, so hopefully you don't have using namespace std; in you r code.

Avoid having magic numbers like 20 in your code, make them const variables instead, and use that variable name in the code.

Good Luck !!


You're missing some crucial information. How are these functions called? What are the arguments fed to them? How are those defined?
Sorry. Here is the rest of my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <iomanip>

using namespace std;
//prototype section
void programInfo();
void inputList(double values[], int &count);
void printList(double values[], int n, int valuesPerLine);
//void sortDescending(double values[], int n);
//double avgList(double values[], int n);
//double stdDev(double values[], int n);
//double medSortList(double values[], int n);
//void printStats(double max, double avg, double std);

int main()
{
	const int MAX_VALUES = 20;
	double values[20];
	int n = 0;
	int valuesPerLine = 0;
	double max = 0;
	double avg = 0;
	double med = 0;
	double std = 0;
	int count = 0;

	programInfo();
	inputList(values, count);
	printList(values, n, valuesPerLine);
//	sortDescending(values, n);
	//	avgList(values, n);
	//	stdDev(values, n);
	//	medSortList(values, n);
	//	printStats(max, avg, std);

	return 0;
}

//definitions section
void programInfo()
{
	cout << "Edwin's statistical calculator." << endl;
	cout << endl;
	cout << "Please follow instruction carefully." << endl;
	cout << "Enter a value at a time (at least 3 to up to 20)." << endl;
	cout << "You must enter valid data or the program will not work." << endl;
	cout << "Enter -1 to signal end of data (-1 or -1.0)." << endl;
	cout << endl;
}

void inputList(double values[], int &count)
{
	
	for (count = 0; count <= 20 - 1;)
	{
		cout << "Enter a value: " << endl;
		cin >> values[count];

		if (values[count] != -1)
		{
			count++;
		}
		else
		{
			break;
		}
	}
}

void printList(double values[], int n, int valuesPerLine)
{
	int count = n;
	for (count = 0; count <= n; count++)
	{
		cout << values[count];
	}
}

I can see what the problem is, but I want you to make the changes I suggested and be able to see them in your edited code in your last post, before I tell what the answer is.

:+)
Last edited on
So I got it to output the correct values, but now since the program lets the user choose whether they want to enter at least 3 values up to a maximum of 20, if I don't enter all 20 values, it outputs weird numbers.

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void inputList(double values[], int &count, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter <= values1 -1; counter = counter + 1)
	{
		cout << "Enter a value: " << endl;
		cin >> values[counter];

		if (values[counter] != -1)
		{
			count++;
		}
		else
		{
			break;
		}
	}
}

void printList(double values[], int n, int valuesPerLine, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter <= values1 - 1; counter = counter + 1)
	{
		cout << "The values that you entered are: " << values[counter] << endl;
	}
}


I think there's something wrong with the for loop in the printList, but I'm not too sure what is.

This is the output when I don't enter all 20 values:

Edwin's statistical calculator.

Please follow instruction carefully.
Enter a value at a time (at least 3 to up to 20).
You must enter valid data or the program will not work.
Enter -1 to signal end of data (-1 or -1.0).

Enter a value:
9
Enter a value:
8
Enter a value:
7
Enter a value:
6
Enter a value:
5
Enter a value:
4
Enter a value:
3
Enter a value:
2
Enter a value:
1
Enter a value:
-1
The values that you entered are: 9
The values that you entered are: 8
The values that you entered are: 7
The values that you entered are: 6
The values that you entered are: 5
The values that you entered are: 4
The values that you entered are: 3
The values that you entered are: 2
The values that you entered are: 1
The values that you entered are: -1 // the -1 is supposed to be the sentinel loop to stop inputting of data. Is it supposed to show up in the output like this?
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
The values that you entered are: -9.25596e+61
Press any key to continue . . .


Can anyone give me some information about what I did wrong in the printList module?
Last edited on
etsuper258 wrote:
Can anyone give me some information about what I did wrong in the printList module?


TheIdeasMan wrote:
I can see what the problem is, but I want you to make the changes I suggested and be able to see them in your edited code in your last post, before I tell what the answer is.


A further hint: For Loop
I feel like it has something to do with the values1 - 1. Is it because values1 = MAX_VALUES which is 20, so it prints all 20 values?

for (counter = 0; counter < values1 - 1; counter = counter + 1)
TheIdeasMan wrote:
The correct form of a for loop is :

1
2
3
for (int Counter = 0; Counter < n; ++Counter) {

}


Now if you change all your for loops, I will give more clues as to what is going on :+)

Is it because values1 = MAX_VALUES which is 20, so it prints all 20 values?


There is something else going on as well.
Last edited on
So like this right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void inputList(double values[], int &count, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter < values1 - 1; ++counter) // can i have the values1 - 1?
	{
		cout << "Enter a value: " << endl;
		cin >> values[counter];

		if (values[counter] != -1)
		{
			count++;
		}
		else
		{
			break;
		}
	}
}

void printList(double values[], int n, int valuesPerLine, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter < values1 - 1; ++counter)
	{
		cout << "The values that you entered are: " << values[counter] << endl;
	}
}
Right, now what is the code which calls these functions, specifically what are the values those functions are called with?

can i have the values1 - 1?


Once we have the correct value, you will see there is no need to subtract 1 from it.

In this code, why don't I subtract 1?

1
2
3
4
5
6
7
8
9
10
const unsigned int SIZE = 10;

double MyArray[SIZE] = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
double Sum = 0.0;

for (unsigned int Counter = 0; Counter < SIZE; ++Counter) {
     std::cout << "Element " << Counter << "is " << MyArray[Counter] << "\n";
     Sum += MyArray[Counter];
     std::cout << "Cumlative Sum is " << Sum  << "\n";
}



Because there is supposed to be 10 values in the array?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void inputList(double values[], int &count, int MAX_VALUES);
void printList(double values[], int n, int valuesPerLine,int MAX_VALUES);

void inputList(double values[], int &count, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter <= values1 - 1; ++counter)
	{
		cout << "Enter a value: " << endl;
		cin >> values[counter];

		if (values[counter] != -1)
		{
			count++;
		}
		else
		{
			break;
		}
	}
}

void printList(double values[], int n, int valuesPerLine, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter < values1 - 1; ++counter)
	{
		cout << values[counter] << "  ";
		if (counter % 6 == 0);
		cout << endl;
		//cout << "The values that you entered are: " << values[counter] << endl;
	}
}

Last edited on
Bloody hell, this is tough - you changed the loops back again - am I wasting my time? I have other things I could be doing. Would you be upset if I suggested you are trolling?

.... specifically what are the values those functions are called with?


You know what a value is, right? Half answering is another troll tactic.

This is a typical disingenuous troll answer:
Because there is supposed to be 10 values in the array?


I will ask the question in a different manner:

What determines when a for loop ends? In other words, how is the end condition specified? How does this mean we don't have to subtract 1? Just trying to get you to think

If you are not trolling, I would want a pretty good reply before I decide to bother replying any further.

I've been trying to get this project to work since 10:30 this morning so any help is appreciated. Sorry if it's frustrating that I don't understand.

To try and answer your question though, if the end condition is false then the for loop ends am I correct? My professor taught us to subtract 1, but I'm really not sure why...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
void inputList(double values[], int &count, int MAX_VALUES);
void printList(double values[], int n, int valuesPerLine,int MAX_VALUES);

void inputList(double values[], int &count, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter <= values1; ++counter)
	{
		cout << "Enter a value: " << endl;
		cin >> values[counter];

		if (values[counter] != -1)
		{
			count++;
		}
		else
		{
			break;
		}
	}
}

void printList(double values[], int n, int valuesPerLine, int MAX_VALUES)
{
	int counter = 0;
	int values1 = MAX_VALUES;

	for (counter = 0; counter < values1; ++counter)
	{
		cout << values[counter] << "  ";
		if (counter % 6 == 0);
		cout << endl;
		//cout << "The values that you entered are: " << values[counter] << endl;
	}
}


It's supposed to be like that right?
I
've been trying to get this project to work since 10:30 this morning so any help is appreciated.


And I have had enough of saying the same things over and over, so that's it from me I am done.
Thanks for your help anyway!
Topic archived. No new replies allowed.