I need help with this C++ HOMEWORK. COSC 246 Homework #3 Due: April 9, 2016 This
ID: 3686494 • Letter: I
Question
I need help with this C++ HOMEWORK.
COSC 246 Homework #3 Due: April 9, 2016
This homework must be submitted online by 11:59 pm on the due date. The solutions will be posted the next day. 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
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.
string fileName;
cout<<"Enter the file name to read from: ";
cin>>fileName;
ifstream fileStream;
while(!fileStream.open(fileName))
{
cout<<"Enter the file name to read from: ";
cin>>fileName;
}
Q2- What output will be produced when the following lines are executed? (Be precise!)
cout.width (5); //Prints the width to a default of 5.
cout << “+” << 2 <<edl; //Prints the text +2.
cout <<setw(5) << “+” << 2 << endl; //Prints the text +2.
Q3- Read a phrase from the keyboard and display how many spaces are in the phrase.
string phrase; //Reads a phrase
cout<<"Enter the phrase: ";
cin>>phrase;
int count = 0; //Initializes the space count to 0.
for(int i = 0; i < phrase.length(); i++) //For each character in the phrase.
if(phrase.at(i) == '')
count++;
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.
int n1, n2;
cout<<"Enter 2 whole numbers: ";
cin>>n1>>n2;
ofstream fStr;
fStr.open("Q4.txt");
fStr<<n1<<" + "<<n2<<" = "<<n1+n2;
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++) //For 4 elements, the array index starts with 0, and will go upto 4 - 1 = 3. This is the fault.
cout << numbers[index] << endl;