472,806 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,806 software developers and data experts.

Summation Questions

2
1. Given that two int variables, total and amount , have been declared, write a sequence of statements that:
- initializes total to 0
- reads three values into amount , one at a time.

After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ).


>>so, first I have to initialize total to 0
total = 0;

>>then I don't know what to do to "read three values into amount, one at a time,"

>>After reading the values, total becomes
total +=amount;

So, does the code altogether becomes
total=0;
(reading the value)
total+=amount;
?



2. Given that two int variables, total and amount , have been declared, write a loop that reads non-negative values into amount and adds them into total . The loop terminates when a value less than 0 is read into amount . Don't forget to initialize total to 0.


This is the code I wrote which I know is not correct..

total =0;
for (amount = 1; amount > 0; amount++)
{
total +=amount
}

Please help me. ..I'm so confused..
Nov 17 '07 #1
4 12132
sicarie
4,677 Expert Mod 4TB
1. Given that two int variables, total and amount , have been declared, write a sequence of statements that:
- initializes total to 0
- reads three values into amount , one at a time.

After each value is read in to amount , it is added to the value in total (that is, total is incremented by the value in amount ).


>>so, first I have to initialize total to 0
total = 0;

>>then I don't know what to do to "read three values into amount, one at a time,"

>>After reading the values, total becomes
total +=amount;

So, does the code altogether becomes
total=0;
(reading the value)
total+=amount;
?
So what do your class notes and your book say about reading integers in? What are some ways to do it? What do you need to do before you just read the variables in (hint: you would have had to do it to total before you set it to 0)

2. Given that two int variables, total and amount , have been declared, write a loop that reads non-negative values into amount and adds them into total . The loop terminates when a value less than 0 is read into amount . Don't forget to initialize total to 0.


This is the code I wrote which I know is not correct..

total =0;
for (amount = 1; amount > 0; amount++)
{
total +=amount
}

Please help me. ..I'm so confused..
So you know your code is not correct there, i see two specific procedural steps you need to take before you add amount to total. What are they? (Remember, compilers are not smart, they only do EXACTLY what you tell them. No more, no less).
Nov 18 '07 #2
msle3
2
When you said I needed to do something to read the integer, did you mean I had to do "scanf ("%d", &total);" first?

How can I read three values into one integer, one at a time then? Do I just use three random values?
Nov 18 '07 #3
sicarie
4,677 Expert Mod 4TB
When you said I needed to do something to read the integer, did you mean I had to do "scanf ("%d", &total);" first?
I would recommend trying it. There is no substitute for experience and playing around with the code yourself. Have you set up any sort of actual program that you can play around with this and test it yourself? (PS - Yes, you are right - something along those lines, though I'm not sure that what you have there will work properly the first time)

How can I read three values into one integer, one at a time then? Do I just use three random values?
Your loop will take care of that. Just think of it procedurally. Right now your loop is set to execute infinitely.

Expand|Select|Wrap|Line Numbers
  1. total =0;
  2. for (amount = 1; amount > 0; amount++)
  3. {
  4. total +=amount
  5. }
So you set amount to 1, and then as long as it is above 0, it will execute, adding 1 each time. I would recommend using a while loop there, then you can test 'while amount is not zero'. Then, you need to do three things each time. What are those three things?
Nov 18 '07 #4
oler1s
671 Expert 512MB
You can't do question two if you aren't solid on question one. So try getting that done correctly. Getting question one done means writing a compileable program that runs properly. So a few random statements here and there don't qualify. You need things like includes, main, declaring your variables, and so on.

And programming isn't something you stare out and instantaneously figure out the code. You are going to have to sit and type, and you might make mistakes. You're going to have fiddle with the code...
Nov 18 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: softwareengineer2006 | last post by:
All Interview Questions And Answers 10000 Interview Questions And Answers(C,C++,JAVA,DOTNET,Oracle,SAP) I have listed over 10000 interview questions asked in interview/placement test papers for...
0
by: connectrajesh | last post by:
INTERVIEWINFO.NET http://www.interviewinfo.net FREE WEB SITE AND SERVICE FOR JOB SEEKERS /FRESH GRADUATES NO ADVERTISEMENT
2
by: freepdfforjobs | last post by:
Full eBook with 4000 C#, JAVA,.NET and SQL Server Interview questions http://www.questpond.com/SampleInterviewQuestionBook.zip Download the JAVA , .NET and SQL Server interview sheet and rate...
4
by: Drew | last post by:
I posted this to the asp.db group, but it doesn't look like there is much activity on there, also I noticed that there are a bunch of posts on here pertaining to database and asp. Sorry for...
1
by: crazydrummer | last post by:
hi everyone, fairly new to access and VBA, but have fundamental knowledge. as part of a college project i am building a database that records "downtime" for machines in a company. the...
7
abouddan
by: abouddan | last post by:
Hi all I am using Access2000 and I am creating a report based on a query. Query1=" select Id,name,Amount from Eployees" This query returns many records, so in the report this result must...
6
by: logieen | last post by:
Hi , Every one I have a problem to solve this summation in c++ ; 1/3 + 3/5 +5/7 ....... this the program I wrote #include <stdio.h> #include <iostream.h> #include <conio.h> #include...
25
by: Darsin | last post by:
Hi all I need to perform a summation on a column of a table based on a criteria given in another column in the same table. The catch is i need to perform different sums according to the number of...
32
by: =?ISO-8859-1?Q?Szabolcs_Horv=E1t?= | last post by:
I did the following calculation: Generated a list of a million random numbers between 0 and 1, constructed a new list by subtracting the mean value from each number, and then calculated the mean...
2
by: galaxianape | last post by:
Please help me with summation in C lang. I m trying to divide the summation of alpha * delta with summation of alpha this is half the program that i have written...but it gives error. pls help.....
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.