Homework help

Assignment:

Write a program segment to store a list of ten integers (input by the user from the keypad) in an array; then display a table similar to the following showing each array element value and what percentage each values is of the total of all ten values in the array.

I wrote the program but I am getting errors. Do i need to put something in my library to make this work?

My errors:

(15) : error C2065: 's' : undeclared identifier
(15) : error C2059: syntax error : 'bad suffix on number'
(15) : error C2017: illegal escape sequence
(15) : error C2146: syntax error : missing ')' before identifier 's'
(15) : error C2001: newline in constant
(17) : warning C4552: '<' : operator has no effect; expected operator with side-effect
(17) : error C2143: syntax error : missing ';' before ')'
(17) : error C2143: syntax error : missing ';' before ')'
(17) : error C2143: syntax error : missing ';' before '{'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  #include "Homework.h"
using namespace std;
int main (void)
	{
	   int n[ 30 ];
	   int i;

	   for ( i = 0; i < 30; i++ ) {
		   n[ i ] = 0;
	   }

	   printf( %s%13s\n", "Element", "Value" ) ;

		   for ( i = 0; i < 30; i++ ) {
			   printf( "%7d%13d\n", i, n[ i ] ) ;
		   }
		   
	   system("pause");
	  return 0;
	}
 


I have in my library now:

 
#include <iostream> 
This was the similar table:

Element value % of total
----------------------------
8 4.00
12 6.00
18 9.00
25 12.50
24 12.00
30 15.00
28 14.00
22 11.00
23 11.50
10 5.00
I was missing a quotation mark. but now when I compile the program it list the elements 0 - 29 and the vakues are 0's ?
well look at line 9. You are givin them all a value of 0. You probably meant cin >> n[i]; or something otherwise it might be hard for you to get values in the array.

*On a side note line 12 can be written as
1
2
3
4
5
6
//c
printf( "Element value %% of total\n" );
//c++
cout << "Element value % of total\n";
//or endl version ( which flushes after new line )
cout << "Element value % of total" << endl;
Last edited on
Where's your input?
What do you mean my input? My code is above
He means user input as I mentioned in my post. You are not getting a user input for your array you are just assigning a value of zero. You want the user to type in each number.
When I changed the code to yours. I got nothing back but a empty screen.

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 "Homework.h"

using namespace std;

int main ()
	{
	   int n[ 10 ]  ;
	   int i;

	   for ( i = 0; i < 10; i++ ) {
		   cin >> n[ i ] ;
	   }

	  
       cout << "Element value % of total\n";

	   for ( i = 0; i < 10; i++ ) {
	   printf( "%7d%13d\n", i, n[ i ] ) ;
		   }
		   
	   system("pause");
	  return 0;
	}
did you input the 10 numbers? Your input should have looked like this
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
with any numbers you wanted. I would also advise you to tell the user they are supposed to input something
So right before line 11 ( inside the for loop ) output something telling them to input a number. ex
 
cout << "Please enter number " << i + 1 << " : ";


or the printf version which ever you prefer.
Ok, so I changed my code to this. I assuming this is what my instructor is asking for.

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
#include "Homework.h"

using namespace std;

int main ()
	{
	   int n[ 10 ]  ;
	   int i;

	   cout << "Please enter 10 numbers: ";
	   for ( i = 0; i < 10; i++ ) {
		   cin >> n[ i ] ;
	   }

	  
       cout << "Element value % of total\n";

	   for ( i = 0; i < 10; i++ ) {
	   printf( "%7d%13d\n", i, n[ i ] ) ;
		   }
		   
	   system("pause");
	  return 0;
	}


this is the output when compiled:


Please enter 10 numbers: 1
3
23
12
15
65
43
67
56
9
Element value % of total
0 1
1 3
2 23
3 12
4 15
5 65
6 43
7 67
8 56
9 9
Press any key to continue . . .

this code does not give me:

"then display a table similar to the following showing each array element value and what percentage each values is of the total of all ten values in the array."
The table isn't printing like the example because you're printing the element location, then the item in that location. What is the table supposed to be displaying?
He asked you to display a table though also you are outtputing the index not the element value.

What I might suggest is you separate the value and % a little more than just 1 space.

so it would look something like


Element value    % of total
3                         1
23                       7.8
12
15
65
43
67
56
9


Also your percent thing seems to be off. You have to add up all the numbers then the percent is x / total

So Here's how I would do it

With your first loop (input )

1
2
3
4
5
6
7
8
9
10
11
12
13
int total = 0;
for( int i = 0; i < SIZE; ++i )
{
    cout << "Please enter number " << i + 1 << ": ";
    cin >> n[i];
    total += n[i];
}

cout << "Element value\t% of total" << endl;
for( int i = 0; i < SIZE; ++i )
{
    cout << n[i] << '\t' << n[i] / (double)total << endl;
}


You could use printf to make the table neater though but I have to go to class so don't have time right now.
Thanks for help. This is why we are called "BEGINNERS"! :)

My Code and yours and works:

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
#include "Homework.h"

using namespace std;

int main ()
	{
	   int n[ 10 ]  ;
	   int i;

	   
	   int total = 0;
for( int i = 0; i < 10; ++i )
{
    cout << "Please enter number " << i + 1 << ": ";
    cin >> n[i];
    total += n[i];
}

cout << "Element value\t% of total" << endl;
for( int i = 0; i < 10; ++i )
{
    cout << n[i] << '\t' << n[i] / (double)total << endl;
}
	   system("pause");
	  return 0;
	}


Compiled:

Please enter number 1: 4
Please enter number 2: 2
Please enter number 3: 7
Please enter number 4: 8
Please enter number 5: 9
Please enter number 6: 12
Please enter number 7: 14
Please enter number 8: 45
Please enter number 9: 56
Please enter number 10: 57
Element value % of total
4 0.0186916
2 0.00934579
7 0.0327103
8 0.0373832
9 0.0420561
12 0.0560748
14 0.0654206
45 0.21028
56 0.261682
57 0.266355
Press any key to continue . . .
Don't forget that to make it a percentage, you have to multiply times 100. In order to make it round to the nearest 100th like the table example, use this.

1
2
#include <iomanip>
cout << setiosflags<ios::fixed> << setprecision(2);
Topic archived. No new replies allowed.