Write a C++ program such that its execution will keep asking the user to enter a pair of integers, start and finish, and will terminate when either start>finish, or EOF or an invalid input has been encountered. Every time a new pair of integers start and finish are read, the program will first calculate the subtotal
start2 + (start+1)2 + (start+2)2 + ... + (finish-1)2 + finish2
and then add the subtotal to the total. Before the program terminates, it should display the accumulated total and the average of all the subtotals.
Thats what i have to do. So far i have typed up a script from what i think maybe right, i'm still quite new to the concept of programming and would require some help. If possible could i get some pointers hints or anything to help me out instead of a direct code as i want to learn my self instead of being spoon fed. So far the program i have written:
#include <iostream>
usingnamespace std;
int main ()
{
int start, start1, finish, finish1, subtotal, total;
cout << "Enter the value for start " << endl;
cin >> start;
cout << "Enter the value for finish " << endl;
cin >> finish;
total = start + finish;
cout << "Total = " << total << endl;
while(start < finish)
{
cout << "Enter another value for start " << endl;
cin >> start1;
cout << "Enter another value for finish " << endl;
cin >> finish1;
subtotal = start + start1 + finish + finish1;
total = subtotal;
cout << "The total value of the integers entered = " << total << endl;
}
cout << "The program has ended" << endl;
system ("pause");
return 0;
}
I have re-entered the code it works although part of my programming is still missing. The part i am talking about is that i want the total to sum up all the values i have entered E.g. 5 and 7 are start and finish values. 6 and 8 are start1 and finish1 values. I want those 2 values added which it does now what if i keep going and entering values is there a way to have all values give me a total? Please let me know if it needs to be explained clearer.
Your start and finish are not 2, it can be 10 or 100. So you can't make 100 start and finish variables. So you need only one start and one finish variable. Sum them and change they values, when sum them again.
you add iteration number and square them. If I good understood your formula.
Could I write your formula like that? (start0 +0)2 + (finish0 -0)2 +(start1 +1)2 + (finish1 -1)2 +...+ (startn +n)2 + (finishn -n)2
Hey thank you for your reply again really appreciate you taking the time to help me out. Also please post up the code if you think it will help explain it better i just wanted some hints and pointers but i don't mind because i have started on the program a bit and you have been a really big help. Well the last part is correct but i am not entirely sure on how to do that but i am trying my best to figure it out as i said quite a beginner at this doing programming fundamentals but haven't done much in it in regards to the topic. Also this will help explain a bit better i hope, i am confused on this my self maybe you can help.
NOTE: If you enter 3 for start and 9 for finish, then the above formula for the subtotal becomes
32 + 42 + 52 + 62 + 72 + 82 + 92
which is 280. The following is the screenshot of one possible execution session (and your implementation may of course differ):
NOTE:
Enter integers for start and finish in pairs until
start > finish, which terminates the program
or when EOF or an invalid input has been made.
Enter start and finish -> 3 9
Enter start and finish -> 1 5
Enter start and finish -> 0 -1
If my rewritten function are right when you do it not right
subtotal = start + start1 + finish + finish1;
You sum it, but you need to calculate a formula, not to sum.
First thing in solving programing problems is to know how it should work in paper. You write all steps you need to do to solve problem on paper, or in mind (better on paper). And don't think about programing.
Your program needs two while loops. First loop for entering values for start and finish variables. Second loop counts subtotal.
And you need 5 variables. start, finish, subtotal, total and iter (to count how moch time was entered start and finish variables). So you could count average for subtotal.
And one variable (six variable) to stop first loop.
Thanks for the update i will try to get what i can done today, but i will get back to you tomorrow as i have to study for an upcoming test. Thank you for your help once again and hope you are able to help me again when need be.
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
int start = 0, start1, finish = 0, finish1, sum = 0, i = 0, j = 0, total = 0, average = 0;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
while (start < finish)
{
while (start <= finish)
{
i = start;
i = (i * i);
cout << "The value of start now = " << start << " The squared value of the value = " << i << endl;
start ++;
sum = sum + i;
}
cout << "The sum of all the squared values = " << sum << endl;
total = sum + total;
//sum = 0;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
}
cout << "Start is greater then Finish " << total << "Average " << average << endl;
system ("pause");
return 0;
}
That's my code what i have done thus far. It is correct almost all done now my only issue is that i have to get the average. The average is only meant to be of the numbers where start is less then finish. So for example lets say i have a total of 335 and the last 2 value i enter are only 2 values. So the average would be equivalent to 167.5. If possible are you able to help me out with whats missing.
You are already very close to your goal. To count an average off subtotal you need to know how much subtotals were calculated. So you need a counter. It seems you don't use variable "j", so let as make it in to your counter. just put "j++;" in line 20. And divide your total from it at the end of program.
P.S. when you get more experience your code will be shorter and faster.
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
int start = 0, start1, finish = 0, finish1, sum = 0, i = 0, j = 0, total = 0, average = 0;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
while (start < finish)
{
while (start <= finish)
{
i = start;
i = (i * i);
cout << "The value of start now = " << start << " The squared value of the value = " << i << endl;
start ++;
sum = sum + i;
}
j++;
cout << "The sum of all the squared values = " << sum << endl;
total = sum + total;
//sum = 0;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
}
average = j / total;
cout << "Start is greater then Finish, The total = " << "The Average = " << average << total << endl;
system ("pause");
return 0;
}
Hey thank you for getting back to me, you have no idea how much i appreciate it. I did what you said and the output is different to what is required. I will give an example and hope it will clarify as to what needs to be done. First input for start 3, first input for finish 5. so 3 squared + 4 squared + 5 squared = 50. Second input for start 5 and second input for finish 7. So 5 squared + 6 squared + 7 squared = 110. Now third input for start 2 third input for finish -3. So 2 squared + 1 squared + 0 squared + -1 squared + -2 squared + -3 squared = 19. Now average is to be 179 / (divided by) last amount of numbers entered which there are 6 numbers so 179/6 = 29.83. Hope that clears it up a bit.
Yeh sorry my bad i had it wrong, thanks for correcting. At first if i enter 2 -3, the program will not respond and close it self. If i enter
2 5
5 7
2 -3
The program runs fine although the average is displayed at 0. The code you see above is unchanged as well. I will be getting back to tomorrow and thank you again.
First: you need to divide your total from counter, not other way.
average = total / j;
Second: you are printing << average << total
if total = 280, and average = 140, you print 140280. This is not right, is it?
Third: you do not clear your sum before using the second time. So you have it like this:
Enter start and finish -> 3 9
Enter start and finish -> 1 5
Enter start and finish -> 0 -1
sum = 280;
sum = (280 + 55) = 335
total = 280 + (280 + 55) = 615
average = 615 / 2 = 307
Hint: this bug can make your program shorter and faster if used right.
Well part of this is right but the total of the inputs is meant to be 335 not 615, any ideas as to how i can change that otherwise its running pretty much fine.
You can do it in two ways.
1) simply delete variable total. And use variable sum in it's place. As Variable sum now stores value of total.
2) You can clear your variable sum from it's value at the end of loop (after line 25). It is done by assign to sum value zero.
#include <iostream>
#include <cmath>
usingnamespace std;
int main ()
{
int start = 0, start1, finish = 0, finish1, sum = 0, i = 0, j = 0, total = 0, average = 0;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
while (start < finish)
{
while (start <= finish)
{
i = start;
i = (i * i);
cout << "The value of start now = " << start << " The squared value of the value = " << i << endl;
start ++;
sum = sum + i;
}
j++;
cout << "The sum of all the squared values = " << sum << endl;
total = sum + total;
cout << "Enter two integers for start and finish" << endl;
cin >> start >> finish;
}
if (j > 0)
{
average = total / j;
}
else
{
average = 0;
}
cout << "Start is greater then Finish, The total = " << total << " The Average = " << average << endl;
system ("pause");
return 0;
}