1. Write a complete C++ program that would display your name and "Saint
Augustine's College" on twolines of the screen.
#include <iostream.h>
void main()
{ cout << "John Smith" << endl;
cout << "Saint Augustine's College" << endl;
}
2. Declare the variables I and J as integer, Alpha and Beta as character,
Apple as a real value, and Name as astring.
int I, J
char Alpha, Beta;
float Apple;
char Name[80];
3. Write a statement that would assign two times the sum of A and B to the
variable A.
A = 2*(A+B)
4. Write a statement that would decrement the variable Count by one if the
value of Alpha is less than 100 andincrement the variable Count by one otherwise.
if (Alpha < 100)
Count--;
else
Count ++;
5. Using a do-while loop, write a sequence of statements that would read
the value of Alpha from the keyboard andadd its value to SUM. Exit the loop
when the value entered for Alpha is zero. Be sure that Alpha and SUM have
proper values whenever they are used.
SUM = 0;
do {
cin >> Alpha;
SUM = SUM + Alpha;
} while (Alpha != 0)
6. Write a sequence that would read exactly 10 numbers from the keyboard
and add them.
Sum = 0;
if (J=1; J <= 10; J++)
{ cin << N;
Sum = Sum + N;
}
7. Write a function that would return the perimeter of a rectangle when its
length and width are given as parameters.
float Perimeter(float Length, float Width)
{ return (2*Length + 2*Width); }
8. Write a sequence of statements that would prompt the user for his name
and read his name from the keyboard.
cout << "Please enter your name";
cin.getline(Name, 80, '\n');
9. Declare RandomArray as an real array containing 100,000 elements.
float RandomArray[100000]
10. Write a procedure (void function) that would fill an integer array with
N random numbers.
void FillArray(int A[]; int N)
{ srand(Time(0));
for(int J=0; J < N; J++)
A[J] = rand();
}
11. Write a statement that would declare a text file variable.
fstream TextFile;
12. Write a statement that would open a text file for output.
TextFile.open("Output.txt", ios::out);
13. Write a statement that would open a text file for input.
TextFile.open("Input.txt", ios::in);
14. Write a statement that would write element J of the array RandomArray
to the opened text file then write anend of line character.
TextFile << RandomArray[J] << endl;
15. Write a statement that would close a text file.
TextFile.close();
16. Write a procedure that would write N elements of an integer array to
a text file. The parameters of the procedureare the name of the file (a
string), the integer array, and the number of elements to be written.
void WriteArray(char FileName[], int A[], int N)
{ fstream F;
F.open(FileName, ios::out);
for(int J = 0; J < N; J++)
F << A[J];
F.close();
}
17. Consider the following list of numbers: 57, 10, 23, 34, 56, 12, 18, 24,
78, 41
This array is to be sorted from smallest to largest using the bubble sort.
Give the array after each of the firstthree passes.
Pass 1: 10, 23, 34, 56, 12, 18, 24, 57, 41, 78
Pass 2: 10, 23, 34, 12, 18, 24, 56, 41, 57, 78
Pass 3: 10, 23, 12, 18, 24, 34, 41, 56, 57, 78
18. Write a procedure that would sort a real array of N elements from smallest
to largest using the bubble sort. What would have to be changed in your procedure
to sort from largest to smallest?
void BubbleSort(float A[], int N)
{ for(int pass = 1; pass < N; pass++)
for (int J = 1; J < N; J++)
if (A[J-1] > A[J]) // change > to <
to go from largest to smallest
{float Temp = A[J-1];
A[J-1] = A[J];
A[J] = Temp;
}
}
19. Consider the following list of numbers: 25, 27, 34, 33, 12, 18, 24, 78,
41
This array is to be sorted from largest to smallest using the insert sort.
Give the array after each of the firstfour passes.
Pass 1: 27, 25, 34, 33, 12, 18, 24, 78, 41
Pass 2: 34, 27, 25, 33, 12, 18, 24, 78, 41
Pass 3: 34, 33, 27, 25, 12, 18, 24, 78, 41
20. Write a procedure that would sort a real array of N elements from largest
to smallest using the insert sort. What would have to be changed in your
procedure to sort from smallest to largest?
void InsertSort(float A[], int N)
{ for (int pass = 1; pass < N; pass++)
{ float 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;
}
}