C++ Homework programs

Apr 24, 2009 at 1:17pm
I have a few programs I need help with, here are the program exercises and then my code (which is incorrect)

Number 1
"Write a program that asks the user for a positive integer. The program should use a loop to get the sum of all integers from 1 up to the number enetered. For example if the user enters 50, the loop will find hte sum of 1, 2, 3, 4, ....., 50.
*Input validation* Do not accept a negative starting number."

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int num, sum, days; // Initiate the counter

//Get integer
cout << "Enter a positive number ";
cin >> days;

cout << "\nNumber Number Summed\n";
cout << "----------------------------\n";

for (int i = 0; i < days; days--)
cout << days << "\t\t" << (sum+=days) << endl;
system ("pause");
return 0;
}


Number 2
"Write a program that calculates how much a person would earn over a period of time if his/her salary is 1 penny for the first day and 2 pennies for the second day, and continues to double each day. The program should ask the user for the number of days. Display a table showing how much the salary was for each day and then show teh total pay at teh end of the period. The output should be displayed in a dollar ammount, not the number of pennies.
*Input vaildation* Do not accept a number less than 1 for hte number of days worked."

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int days, salary; // Initiate the counter
double total = 0.0;

//Get integer
cout << "Enter a positive number\n";
cin >> days;
for (; days > 0; days--)
{
(total+=days*2);
}

//Display Total
cout << fixed << showpoint << setprecision(0);
cout << "The salary is $:" << total << endl;
system ("pause");
return 0;
}


Number 3
"Write a program that will predict the size of a population of organisms. The program should ask the user for hte starting number of organisms, their average daily population increase (as a percentage), and the number of days they will multiply. A loop should display the size of the population for each day.
*Input Validation* Do not accept a number less that 2 for the starting size of the population. Do not accept a negative number for the average daily population increase. Do not accept a number less than 1 for the number of days they will multiply."

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int sorg, dinc, dmul, psize;

//Calculations


cout << "Whats the starting organisms population size? ";
cin >> sorg;
cout << "\nWhats the daily average popluation increase (percentage)? ";
cin >> dinc;

cout << "\nWhats the number of days they will multiply? ";
cin >> dmul;
psize = sorg;
for(int count = 2; count <= dmul; count++)
{
psize = psize +(sorg * dinc/100);
cout << "\nThe size of the population is: "<< psize;
}
system ("pause");
return 0;
}
Apr 25, 2009 at 5:39am
Number 1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
main()
{
int num, sum, days; // Initiate the counter
//....this is declaring num,sum and days of type int. It does not initiate or initialize them

//Get integer
cout << "Enter a positive number ";
cin >> days;

cout << "\nNumber Number Summed\n";
cout << "----------------------------\n";

for (int i = 0; i < days; days--)//change to days++
sum+=i;//add
cout << days << "\t\t" << (sum+=days) << endl;//revise to
cout<<"The sum of integers 0 to "<<days<<" is "<<sum<<endl;


I haven't checked this but sum== 1/2n(n+1)==25*51==1275

Number 2
This is essentially the same as problem #1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int days, salary; // Initiate the counter
//as before above line declares days and salary as type int.
double total = 0.0;//this decares and initializes total as a type double......no problem

//Get integer
cout << "Enter a positive number\n";
cin >> days;
for (; days > 0; days--)//rewrite for(int i=0;i<days;i++)
{
(total+=days*2);//total+=i;//this total is in pennies
}

//Display Total
cout << fixed << showpoint << setprecision(0);//not sure what you are trying to do here
cout << "The salary is $:" << total << endl;
//total/100 to produce dollars

Number3.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//include header <cassert>
assert  sorg>1;//input validation
assert dinc>0;//ditto
assert dmul>0;//ditto
int sorg, dinc, dmul, psize;

//Calculations


cout << "Whats the starting organisms population size? ";/* rewrite..."organisms starting population size"*/
cin >> sorg;
cout << "\nWhats the daily average popluation increase (percentage)? ";
cin >> dinc;

cout << "\nWhats the number of days they will multiply? ";
cin >> dmul;
psize = sorg;
for(int count = 2; count <= dmul; count++)//start count at 0
{
psize = psize +(sorg * dinc/100);//think this should be psize*=dinc/100 but you should check
cout << "\nThe size of the population is: "<< psize;
}

hth's





Apr 26, 2009 at 10:15am
for the first problem...

your code is good with days-- but a its a bit harder to undersand. Normally you would want to add the numbers from 1 to days so you need:
for(int i=1;i<=days;i++) {sum+=i;//some couts... }

and it says that you have to check if the number is negative, so after cin>>days; you should put:
if(days<0){cout<<"blabla your number is bad blabla";return 0;}
Topic archived. No new replies allowed.