I need help with C++ HOMEWORK. Due: April 9, 2016 ** For questions that ask you
ID: 3687094 • Letter: I
Question
I need help with C++ HOMEWORK.
Due: April 9, 2016
** 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- 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.
Q2- What output will be produced when the following lines are executed? (Be precise!)
cout.width (5);
cout << “+” << 2 <<edl;
cout <<setw(5) << “+” << 2 << endl;
Q3- Read a phrase from the keyboard and display how many spaces are in the phrase.
Q4- 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 ‘Q4.txt’. The program should write to the end of ‘Q4.txt’ if the file already exists.
Q5- This code segment is supposed to display the elements of the array ‘numbers’ one per line, but it doesn’t work. Find the error and then show how you would fix it.
int numbers [] = {10,20,5,100};
for (int index=0 ; index <= 4; index++)
cout << numbers[index] << endl;
Q6- Define an int array of size 20 and initialize every element of the array with a random number in the range of 10 through 50.
Q7- What is the output of the following code segment?
int num [5];
int sum =0;
for (int n=0 ; n< 5; n ++)
num[n] = n*2;
for (int n=0 ; n< 5; n ++)
if (n%2 == 0)
sum += num[n];
cout <<“sum = “ << sum;
Q8- Write a function called ‘average’ that takes an array of type int and its size as parameters, and returns the average of all the elements in the array. Write a line of code that displays the average of the array ‘num’ defined in Q5 (invoke the function average with the array ‘num’ as an argument).
double average (int array_par[], int size)
{
// This method should work correctly for any array regardless of its size.
}
Q9- Consider the array defined below,
int numbers[] = {10, 20, 15, 8, 29, 90, 50};
a- Write a code segment to output the last three elements of this array (use a loop).
b- What is the index of the smallest element in this array?
c- Suppose you are going to sort this array using the selection Sort algorithm. Show the contents of the array ‘numbers’ after the second pass through the selection sort algorithm.
Explanation / Answer
Multiple Questions : Answering two quiestions :
Q2. Answer :
+2
+2
Q7. Answer :
sum = 12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.