Question

1.Given an int variable n that has been initialized to a positive value and, in addition,...

1.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a for loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++)

2.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a while loopto compute the sum of the cubes of the first n counting numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total. Do NOT modify n. (c++)

3.Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have already been declared, use a do...whileloop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and total.(c++)

4.

Read first a user's given name followed by the user's age from standard input. Then use an ofstream object named outdata to write this information separated by a space into a file called outdata. Assume that this is the extent of the output that this program will do.

Declare any variables that you need.(c++)

5.
Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percentage, expressed as a fraction in decimal form: for example 0.052 would mean a 5.2% increase each day), 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 than 2 for the starting size of the population. If the user fails to satisfy this print a line with this message "The starting number of organisms must be at least 2.", display the prompt again and try to read the value. Similarly, do not accept a negative number for average daily population increase, using the message "The average daily population increase must be a positive value." and retrying. Finally, do not accept a number less than 1 for the number of days they will multiply and use the message "The number of days must be at least 1."(c++)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, Please post 1 question at a time.

I have solved the first 4, Please post question 5 in a separate post.

As per Chegg policy, I am suppose to answer only the first 4 questions.

Here is the code in C++, do comment for any further help.

thank you !

===================================================================

#include<iostream>

#include<fstream> // for wriitng data to file

using namespace std;

int main(){

               

               

                int n=4;

                int k;

                int total;

               

                // Problem 1

                total=0;

                for(k=1;k<=n;k++){

                                total+= k*k*k;

                }

                cout<<"Problem 1 total = "<<total<<endl;

               

               

                //Problem 2

                k=1;

                total=0;

                while(k<=4){

                                total+=k*k*k;

                                k+=1;

                }

                cout<<"Problem 2 total = "<<total<<endl;

               

                //Probem 3

                k=1;

                total=0;

                do{

                                total+=k*k*k;

                                k+=1;

                }while(k<=4);

                cout<<"Problem 3 total = "<<total<<endl;

               

                // Problem 4

                char filename[]="outdata.txt";

                char username[51];

                int age;

                cout<<"Enter user name: ";

                cin.getline(username,50,'\n');

                cout<<"Enter age: ";

                cin>>age;

               

                ofstream outfile(filename);

                if(outfile.is_open()){

                                outfile<<username<<", "<<age<<endl;

                                outfile.close();

                                cout<<"File updated successully\n";

                }else{

                                cout<<"Unable to write data to file "<<filename<<endl;

                }

}

===================================================================

Add a comment
Know the answer?
Add Answer to:
1.Given an int variable n that has been initialized to a positive value and, in addition,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT