473,405 Members | 2,160 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 12195
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.....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.