Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For questions that ask you to write code just write enough to answer the questio

ID: 3677404 • Letter: F

Question

For questions that ask you to write code just write enough to answer the question. You do not need to write an entire program.

Q1- What is the output of the following code segments?

a)

for (int n=1 ; n != 10; n= n + 2) cout << n <<" ";

b)
int sum =0;
for (int i=0; i<=12; i++) {

if (i%3 != 0) {

cout <<i <<" ";

sum += i; }

}
cout <<"sum = " << sum;

c)
int n=5;

while (n>0) {

for (int i=0; i<5; i++) if (i%2 == 1)

cout <<i << " "; cout << endl;

n= n-2; }

d)
int n = 11; while (n >0) {

n = n– 2 if ( n == 5)

continue; cout <<n <<" ";

}

e) Write a code segment that displays all the odd numbers from10 through 50 four on each line.

Q2- Write a function definition for a function called “is_vowel” that takes an argument of type char. The function returns true if the argument is a vowel, and false otherwise.

Q3- What is the output of the following code segment (Note that the function is_vowel is defined above)?

char letter =’B’;
int num;
while (! is_vowel(letter)) {

num = static_cast <int> (letter) +1; letter = static_cast <char>(num); cout << letter <<" ";

}

Q4- Write a function definition for a function called “sumOfPowers” that takes an argument of type int (n). The function returns the sum of all the powers of 3s staring from 1 through n. (For example, if n is equal to 5, it returns the sum of 31+32+33+34+35).

Q5- Write a void function called “sumOfEven” that takes two arguments of type int. The function finds the sum of all the even numbers between 1 and the value of the first argument, and returns the sum through the second argument.

Q6- Write a code segment that reads a whole number from the keyboard, calls the function “sumOfEven” defined above to compute the sum of all the even numbers between 1 and the value inputted at the keyboard, and then displays the sum.

Q7- Write a code segment that asks the user for an input file name, and allows the user to re-enter the file name if the file doesn’t exists.

Q8- What output will be produced when the following lines are executed? (Be precise!) cout.width (5);

cout << “+” << 2 <<edl;
cout <<setw(5) << “+” << 2 << endl;

Q9- Read a phrase from the keyboard and display how many spaces are in the phrase.

Q10- Write a code segment that prompts for and reads in two whole numbers from the keyboard and writes both numbers and their sum into an output file called ‘Q10.txt’. The program should write to the end of ‘Q10.txt’ if the file already exists.

-------------------------------------------------------------------------------------------------------------------------------------------------------

I'm expecting different answer then these answers below at this time.

1.

a)

output:

1 3 5 7 9

b)

output:

1 2 4 5 7 8 10 11

sum= 48

c)

1   3  

1   3  

1   3

d).

9   7   3   1   -1

e)

//for keeping the count of number of odd numbers printed in a line

int count=0;

for(int i=10;i<=50;++i)

{

   if(i%2==0)

   {

       cout<<i<<" ";

       count++;

   }

   if(count==4)

   {

       //going to next line after every 4 odd numbers in one line

       cout<<endl;

       count=0;

   }

}

2.

bool is_vowel(char c)

{

   if(c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U')

   {

       return true;

   }

   return false;

}

3.

output:

C D E

4.

//using pow function from cmath so you need to include<cmath> at the top of the actual program

long sumOfPowers(int n)

{

   long sum=0;

   for(int i=1;i<=n;i++)

   {

       sum+=pow(3,i);

   }

   return sum;

}

5.

//passing sum by reference so we will update the value of sum with the sum of all even numbers between 1 and n

void sumOfEven(int n,int& sum)

{

   sum=0;

   for(int i=1;i<=n;i++)

   {

       if(i%2==0)

       {

           sum+=i;

       }

   }

}

6.

int n,sum;

//Taking a keyborad input for n

cout<<"Enter a number"<<endl;

cin>>n;

sumOfEven(n,sum);

cout<<"sum of all even numbers from 1 to "<<n<<"is "<<sum;

7.

char* filename;

//will reprompt the user for a filename until he enters a valid file name and we break the while loop in if

while(true)

{

   cout<<"Enter a input file name"<<endl;

   cin>>filename;

   if (std::ifstream(name))

   {

break;

   }

}

8.

output:

  +2

+2

9.

string s;

   cout<<"enter a phrase"<<endl;

   cin>>s;

   int countSpaces=0;

   for(int i=0;i<s.length();++i)

   {

       if(s[i]==' ')

       {

           countSpaces++;

       }

   }

   cout<<"number of spaces in the phrase are"<<countSpaces;

10.

int a,b,sum;

   cout<<"enter two numbers"<<endl;

   cin>>a>>b;

   sum=a+b;

   ofstream file(name);

   file<<"First number "<<a;

   file<<"Second number "<<b;

   file<<"their sum "<<sum;

   file.close();

Explanation / Answer

Q.1

a. 1 3 5 7 9 11 ... infinite loop

b. 1 2 4 5 7 8 10 11 sum = 48

c.

1 3

1 3

1 3

d. 11 9 7 3 1

2.

bool is_vowel(char ch) {

   char vowels[] = {'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U'};

   for (int i = 0; i < 10; ++i) if (ch == vowels[i]) return true;

   return false;

}

3.

C D

4.

int sumOfPowers(int n) {

   int sum = 0;

   for (int i = 1; i <= n; ++i) {

   int toAdd = 1;

   for (int j = 1; j <= i; ++j) toAdd *= 3;

   sum += toAdd;

}

return sum;

}

5.

void sumOfEvens(int n, int &sum) {

sum = 0;

for (int i = 0; i < n; i += 2) sum += i;

}

Please post rest of the questions as separate assignment. Post a single question with no subpart OR question with maximum allowed 4 subparts.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote