Homework help?

Write a program that accepts 6 integers from the user, puts them into an array, and outputs the sum.

This is the sample given by my professor. I have no idea how to change it to max the directions or anything. So any help would be appreciated.

#include<iostream>
using namespace std;

int main()
{
int k = 0, m = 0, arr[5] = {0}, sum = 0;

while(k++ <5)
{
cout<<"enter number "<<endl;
cin>>arr[k];
}

while(m++ <5)
{
sum = sum + arr[m];
}

cout<<sum;

m=4;

while(m-- >=0)
{
cout<<arr[m]<<endl;
}
system("pause");
return 0;
}
Why do yopu declare an array with 5 numbers when you need to store 6 numbers ?
Also since you know that you will use 6 numbers you can use a for loop.
 ``1234567891011121314151617181920`` ``````#include using namespace std; const int MAX_NUMBERS = 6; int main() { int numbers[MAX_NUMBERS] = {0}; for (int i = 0; i < MAX_NUMBERS; i++) { cout << "\nEnter number[" << i << "] "; cin >> numbers[i]; } int sum = 0; // TODO calc and output the sum from numbers return 0; } ``````
Hello CodingIsHard17,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

As Thomas1965 pointed out
 Why do you declare an array with 5 numbers when you need to store 6 numbers ?

And the instructions
 Write a program that accepts 6 integers from the user. Puts them into an array. And outputs the sum.

When I ran the program it stored four numbers not five and not the six that you need.

For the most part the program works, but it does need some changes to work right.

For a start:
 ``12345`` ``````while(k++ < 5) { cout<<"enter number "<>arr[k]; }``````

You should read up on pre and post increment operator and how they work.
http://www.cplusplus.com/doc/tutorial/operators/#incrementwww.cplusplus.com/doc/tutorial/operators/#increment

As it is "k" will only store 4 numbers in the array and element zero of the array is never used. The whole while condition needs fixed. The same will have to be done with the next two while loops and the line `m = 4;` will need to be changed.

`system("pause");` is not the best code to use. Read this for more information:
http://www.cplusplus.com/forum/beginner/1988/

Hope that helps,

Andy
Last edited on
It was using five numbers because it was an example. I thought I stated that. But Thomas1965, the code you provided me with only allows the user to enter 6 numbers. How can I make the output be the sum of those 6 numbers?
Hello CodingIsHard17,

I think we both understand the what you posted is an example.

As I stated earlier the program works. It just needs changed to work properly.

In all three while loops the concept works, but the way they are done is wrong and does not work as is.

I have finished the program and made it work. I have given you a hint about what is wrong "k++" and am waiting to see what you know and understand instead of just giving you the answer.

All I did was to make changes to what you posted without changing the whole code.

What is the point of the exercise? What are you trying to learn about? Arrays, loops or how to use both?

Andy
 the code you provided me with only allows the user to enter 6 numbers

Why is that a problem? That is what the problem specification stated.

 How can I make the output be the sum of those 6 numbers?

Write a `for` loop that adds the numbers in the array.
Last edited on
The original program is hardly an example - it has so many problems it would be better to throw it away and start again. If your professor gave it to you, I certainly hope the instructions were - "see how many errors you can find in this poorly-written code".

The compiler warns of undefined behaviour in two different places:
 ```In function 'int main()': 25 20 [Warning] iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations] 23 15 [Note] within this loop 16 26 [Warning] iteration 4 invokes undefined behavior [-Waggressive-loop-optimizations] 14 15 [Note] within this loop```

Last edited on
I don't know how to write it at all. You see how poorly written my professor examples are. I don't even know where to begin to find out what is wrong with the k++. This is much harder for me than others. Sorry.
 But Thomas1965, the code you provided me with only allows the user to enter 6 numbers. How can I make the output be the sum of those 6 numbers?

Just use a second for loop and add the numbers of the array to the sum.
Then output the sum.
Hello CodingIsHard17,

It sound like you need to revisit your textbook for awhile.

What I was trying to show you earlier:
 Write a program that accepts 6 integers from the user. Puts them into an array. And outputs the sum.

As you see it now looks like three different steps for the program. Work on one and when it is working move on to the next. Do not try to visualize the whole program at once.

Thomas1965 showed you a for loop that will work well for the input.

Work something up and post it. Even if it is wrong we can work on fixing and understanding how it works.

This may be of some help to get you started:
http://www.cplusplus.com/doc/tutorial/control/#for
The rest of the age is also useful. A good page to keep for future reference.

Hope that helps,

Andy
I was able to come up with this after asking around for further explanation:
#include<iostream>
using namespace std;

int main()
{
int arr[6]; // variable declaration
int val = 0;
cout<<"Enter six numbers, pressing enter after each value: \n";

for(int i = 0; i<6; i++)
{
cin>>arr[i];
}

for(int i = 0; i < 6; i++)
val = val + arr[i];
cout<<"The sum of the numbers entered are: "<<val<<endl;

return 0;
}

It works well.
Hello CodingIsHard17,

Very good. Now follow the links from my earlier post about using code tags and you will be one step close. Then we can work on blank lines and indenting to make your code easier to read.

I have revised your code a little to show you a different way of writing this program. Read the comments in the program for what I did.

 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````#include // This is not a problem for this program, but it WILL be in the future. Best to learn using "std::" // early when it is easier. This will help explain the "using namespace xxxx" problem and even when it is // useful http://www.lonecpluspluscoder.com/2012/09/22/i-dont-want-to-see-another-using-namespace-xxx-in-a-header-file-ever-again/ //using namespace std; int main() { // This allows you to change the size of the array and the condition in the for loops from one place. // Making this variable a const allows you to use this name in defining an array which needs a constant // number. Regular variables can ot be used to define the size of an array only a constant number like // "6" or a variable defined as const. // This is defined in main because that is where it is used. A global variable will work, but not necessary. const int MAXSIZE{ 6 }; int arr[MAXSIZE]{}; // variable declaration <--- Added the {} to initialize the array to zeros. int val = 0; std::cout << "Enter six numbers, pressing enter after each value: \n"; for (int i = 0; i < MAXSIZE; i++) { // This line is a nicer way of letting the user know what to enter. Notice the "i + 1" this is because // the for loop starts at zero. std::cout << "\n Enter number " << i + 1 << ". "; // <--- The space at the end and nothing for a new line will allow the cin to be on the same line. std::cin >> arr[i]; } for (int i = 0; i < MAXSIZE; i++) val = val + arr[i]; // <--- This line is indented to show it is part of the for loop. // <--- Blank line here to make it easier to read and show it is not part of the for loop. std::cout << "The sum of the numbers entered are: " << val << std::endl; return 0; }``````

See what you think and let me know if you have any questions. Load it up and run it and see how it compares to what you did. The program will function the same it is just dressed up a bit.

Note: I made the changes, but have not had the chance to test it yet to make sure I did not make any typos or do something stupid. Since I did not make any major changes to the code it should work.

Hope that helps,

Andy

EDIT: Sorry I missed line 21. That is what happens with copy and paste.
Last edited on
Andy answered pretty much what you needed, just need a "std::" added in line 21, or do this:

 ``12345678`` `````` #include // add these here: using std::cin; using std::cout; using std::endl; ``````

Talemache~
Last edited on
Topic archived. No new replies allowed.