1. Write statement sequences that would
a. Read a users name from the keyboard after being prompted for the
name.
cout << "Enter your name: ";b. Declare a file variable and open it for input. The input file name should be “numbers.txt”.
cin.getline(Name, 80, '\n');
fstream InFile;c. Read N integers from the file opened in part b.
InFile.open("numbers.txt", ios::in);
for(int j=0; j < 10; j++)d. Close the file opened in part b.
InFile >> A[j];
InFile.close();2. Write a generic (template) procedure that would sort N elements of the generic array Alpha in descending order using the insert sort.
template <class ItemType>3. Consider the following values of an array:
void InsertSort(ItemType A[], long N)
{ for (int pass = 1; pass < N; pass++)
{ ItemType Temp = A[pass];
int J = pass;
while( (J > 0) && (A[J-1] > Temp)) // change < to > for smallest to largest
{A[J] = A[J-1];
J--;
}
A[J] = Temp;
}
}
Give the first four passes of the array if the sort routine is the bubble sort. The sort order is from largest to smallest.34 53 75 85 15 22 91 48
4. Write a procedure that would obtain from the user the user’s name, the name of an output file, and the user’s age (an integer) as the parameters.pass 1: 53 75 85 34 22 91 48 15
pass 2: 75 85 53 34 91 48 22 15
pass 3: 85 75 53 91 48 34 22 15
pass 4: 85 75 91 53 48 34 22 15
void GetData( char Name[], char FileName[], int &age)5. Write a procedure that would write N numbers from an array to an output file, with 10 numbers per line. The parameters of the procedure are the filename, the array, and the number.
{ cout << "Enter your name: ";
cin.getline(Name, 80, '\n');cout << "Enter the output file name: ");
cin.getline(FileName, 80, '\n');cout << "Enter your age: ");
cin >> age;
}
void WriteData(char FileName[], int NumbArray[], int N)6. Write a C++ header file that would contain the following generic (template) functions and procedures:
{ fstream F;
F.open(FileName, ios::out);for(int j=0; j < N; j++)
{ F << NumbArray[j];
if(j % 10 == 9)
F << endl;
}F.close();
}
a. a generic procedure that would sort an array using the insert sort.7. Write the implementation (code) for the function you gave in part e of number 6.
b. a generic procedure that would sort an array using the bubble sort.
c. a generic procedure that would sort an array using the quick sort.
d. a generic procedure that would sort an array using the heap sort.
e. a generic function that would return true or false that would test an array to see if it is sorted.template <class T>
void InsertSort( T Array[], int N);template <class T>
void BubbleSort( T Array[], int N);template <class T>
void QuickSort( T Array[], int N);template <class T>
void HeapSort( T Array[], int N);template <class T>
bool IsSorted( T Array[], int N);#include "SortRoutines.cpp"
template <class ItemType>
bool IsSorted(ItemType A[], long N)
{ bool Sorted = true;for (int J = 1; J<N; J++)
if (A[J-1] > A[J])
Sorted = false;return Sorted;
}
template <class ItemType>
class stack
{
public:
void push( ItemType );
void pop ();
ItemType top();bool empty();
stack();
~stack();private:
struct node
{
ItemType item;
node *next;
};node* head;
};
9. Give the contents of a stack T after the following operations:
T.Push(24);
T.Push(35);
T.Pop();
T.Push(12);
T.Push(15);
T.Pop();
T.Pop();
T.Push(91);24
35 deleted
12 deleted
15 deleted
91
10. Write a sequence of statements that would do the following items:
a. randomize the random number generator.11. Write a complete C++ program that would read 10 names from the input file “Names.txt” and write the names out onto the screen. You do not need to use a procedure. The most efficient code gets the most points.
b. fill N elements of an array of integers with random numbers.
c. Time the bubble sort on the array of N elements.
d. Write out the results of part c.
e. Test to see if the array you sorted in part c is actually sorted.srand(time(0));
for(int j = 0; j < N; j++)
A[j] = rand();StartTime = time(0);
BubbleSort(A, N);
EndTime = time(0);
ElapsedTime = EndTime - StartTime;cout << "The time to sort "
<< N << " integers using the bubble sort is "
<< ElapsedTime << " seconds" << endl;if (IsSorted(A,N))
cout << "The array was correctly sorted\n";
else
cout << "Error -- the array is not sorted\n";
#include <iostream.h>
#include <fstream.h>void main()
{ char Name[80];fstream F;
F.open("Names.txt", ios::in);for(int j=0; j<10; j++)
{ F.getline(Name, 80, '\n');
cout << Name << endl;
}F.close();
}