I need help with this C++ HOMEWORK. COSC 246 Homework #3 Due: April 9, 2016 This
ID: 3686221 • 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) c++ code for taking existing file name as input.
string inputfilename;
cout << "Enter the name of file ";
cin >> inputfilename ;//getting file name from user
fstream inputfile( inputfilename.c_str() ); //taken file name as file stream
while(!inputfile) //until valid file for opening repeats
{
cout << "doesn't exist input file enter again the correct file name";
cin >> inputfilename ;//getting another file name from user
fstream inputfile( inputfilename.c_str() );// again taken file name as file stream
}
Q2)output for the given code segmen
+2
+2
-> above output will get but ,in the second line ending not "edl" replace with "endl"
-> width(int) method used for returning field width of current value.
-> setw(int) method used for setting field width for output operations.
Q3)c++ code segment for counting the number of spaces in the phrase:
cin.getline(line,max-size);
while(line[i]!='')
{
if(line[i]==' ')
count = count + 1;
}
Q4) c++ code segment for writing output to file called Q4.txt
int num1,num2,sum;
cout << "enter first whole number";
cin >> num1;
cout << "enter second whole number";
cin >> num2;
sum = num1 + num2;
ofstream outputfile;
outputfile.open("Q4.txt");
outputfile << num1;
outputfile << num2;
outputfile << sum;
outputfile.close();
Q5)modified c++ code segment for displaying array contents
-> the numbers[] array has four elements but printing 5 th element in the numbers[] array.
-> change above code segment for printing 4 elements in th array,as shown below.
int numbers [] = {10,20,5,100};
for (int index=0 ; index <= 3; index++)
cout << numbers[index] << endl;
Q6)code segment for generating 20 random numbers in the range of 10 and 50
int ary[20],min=10,max=50;
for(i=0;i<20;i++)
{
ary[i]=(rand()% (max-min+1))+min;
}
Q7)ouptut of c++ code segment:
sum = 12
Q8)c++ code segment for defining average function
int main()
{
int numbers [] = {10,20,5,100};
for (int index=0 ; index <= 4; index++)
cout << numbers[index] << endl;
double average_ = average(numbers[],4);
cout << "average is " << average_;
return 0;
}
double average (int array_par[], int size)
{
}
Q9)
a)
for(i=6;i>3;i--)
cout << numbers[i];
b)
int index=0,i;
for(i=1;i<7;i++)
if(numbers[index]>numbers[i])
index=i;
cout << "the index of the smallest element in the array is: " << index;
c)the contents in the numbers array after 2nd pass using selection sort is displayed below.
numbers[]={8,10,15,20,29,90,50}