Hi!
I am in desperate need of help ... I am new in C + + (and programming overall ...)
I have major problems with an assignment that I have been working on for a long time, without any success, and I can not find information anywhere ...
The task is to build a program that asks for two integers, prints all the numbers between the entered integers and calculates the sum (between the input rate) and the average value and print them.
procedure should also be repeated the user wants, and should be asked if he / she wants to enter two new integer (Y / N), etc. etc. etc.
it should consist of a number of functions, I have managed to make it work with the help of loops, while, for, else / if, etc., but the source code is long and convoluted (only in main ...)
How do I write it with a number of functions and function calls, etc.?
I'm really desperate and would be extremely pleased if someone could help with some advice, code snippets, etc.
void PrintSomething() // this is a function to print "Something"
{
cout << "Something" << endl;
}
void PrintSomethingElse() // this is a function to print "Something Else"
{
cout << "Something Else" << endl;
}
int main()
{
// now let's say we want to print:
// Something
// Something Else
// Something Else
// Something
// we could just call the above functions:
PrintSomething(); // calls our PrintSomething function
PrintSomethingElse();
PrintSomethingElse();
PrintSomething();
}
When we call Something();, the computer temporarily leaves main and jumps over to the "Something" function. It will run through the code in that function. Once that function is done, the computer returns back to the code that called it so it would jump back to that line in main).
In your case, you'd probably want to write functions to do some of the smaller tasks. Like get input from the user, or calculate the sum of all values between two given numbers, or print output to the screen, etc.
int main()
{
char choice = 'y';
while (choice == 'y')
{
int low, high, sum = 0;
float mean;
cout << "enter two integers: ";
cin >> low >> high;
cout << "list of numbers: "for (int i = low; i <= high; i++)
{
cout << i << " ";
sum += i;
}
mean = sum / (high-low+1);
cout << endl << "sum is: " << sum << endl;
cout << "average is: " << mean << endl;
cout << "do it again? (y/n): "
cin >> choice;
}
return 0;
}
Now if we really want to put some functions in there we could just break this down into some segments like Disch mentions. Functions are normally used to reuse code, however I don't really see any code here that is redundant and needs some re-using. Another option is breaking code into functions to make things easier to read. Something like this:
int print_n_sum(int i)
{
cout << i << " ";
return i;
}
void do_loop()
{
int low, high, sum = 0;
float mean;
cout << "enter two integers: ";
cin >> low >> high;
cout << "list of numbers: "for (int step = low; step <= high; step++)
sum += print_n_sum(step);
mean = sum / (high-low+1);
cout << endl << "sum is: " << sum << endl;
cout << "average is: " << mean << endl;
}
int main()
{
char choice = 'y';
while (choice == 'y')
{
do_loop();
cout << "do it again? (y/n): "
cin >> choice;
}
return 0;
}
In this case I put the meat of our main loop into its own function "do_step()" just to seperate it from that boring looping stuff as it isn't really relevant to what we are trying to calculate. It helps to reduce clutter and allows us to focus on what is really happening.
I also made print_n_sum to demonstrate how to return a value which can make something like a for loop easier to read in that "meat and potatoes" function.
THANKS! I think i´ve managed it this far (well, now it's most copy-paste, some small changes (which looks awful (if (high < low) etc. don't know how to write it in another way...)
it's quite similar to the pseudo code i wrote... don't know why i have to use functions, instructions says so..
I've tried and tried and tried and tried but got completely stuck!! But now 1) it works! just have to add comments etc. and change variable names etc. etc. and 2), more important, i (think) i know how to do - have another assignmnt to do before monday (to be able to get an B/A..) which is a little bit more complicated, includes more functions and (maybe) some text strings etc. (well, its a VERY basic course.. but i'm a beginner..)
No, I do not think so ... at least it works on this PC in visual studio c++ (should test it in xcode too..) but if you see something I can not see (im kind of blind atm..), I would be very grateful if you want to correct me and/or comment and say what I should keep in mind ... can't reach my teacher :/