0% found this document useful (0 votes)
18 views

C++ Practise Questions2

The document contains 12 questions on programming concepts for a final exam. The questions cover topics like functions, arrays, structures, files and more. Students are asked to write C++ programs to check prime numbers, convert units like time to hours, calculate series sums, and perform other tasks using appropriate techniques like functions, loops, arrays etc. Sample solutions for some midterm exam questions are also provided covering array processing, function definitions, input validation and more.

Uploaded by

Nduduzo Hlophe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C++ Practise Questions2

The document contains 12 questions on programming concepts for a final exam. The questions cover topics like functions, arrays, structures, files and more. Students are asked to write C++ programs to check prime numbers, convert units like time to hours, calculate series sums, and perform other tasks using appropriate techniques like functions, loops, arrays etc. Sample solutions for some midterm exam questions are also provided covering array processing, function definitions, input validation and more.

Uploaded by

Nduduzo Hlophe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Question Bank for Programming-I for Final Exam

GROUP 1 - Programs
Q1-1\ Write a C++ program that uses a function to check whether a given integer is prime or not.
NOTE: Prime number is the integer divisible only by itself and by 1. The number is considered non-prime if
it is divisible by any other number.

Q1-2\ Write a program to input numbers from the user until the number -100 is encountered. The square of
sum of all numbers should be printed out.

Q1-3\ Write a program that asks for an integer (1-12) representing the month, and depending on the input,
show the month name as a word (in English). Use a switch statement.
For example for 1 it should print Jan

Q1-4\ Write a C++ code to sum the following series:

Q1-5\ Write a C++ program that print the following shape. Use switch

#####
$$$$
***
&&
!
Q1-6\ Assuming an array who has 5 elements with following initial values for time in minitues:
{12.0, 15.0, 24.0, 90.0, 120.0}
Write a C++ program to convert these values in the same array to hours like below
{0.2, 0.25, 0.4, 1.5, 2.0}
You must use a function named Conv_To_Hour that should take the array as input.

Q1-7\ Write a C++ program to square array values use function named Square_Array that should take the
array as input.
For example A = {4, 5, 6, 9, 11}
should be converted to A = {16, 25, 36, 81, 121}

Q1-8\ Write a C++ program to draw the shape below. The program should use a function Draw_Squar that
takes the character and the square side length as input
Q1-9) Write a program that allows the user to enter the grade scored in a class (0-100).
The program should validate the input using do while.

Q1-10)Then notify the user of their letter grade according to below table

Grade Letter Grade


0-59 F
60-69 D
70-79 C
80-89 B
90-100 A

Use nested If statements.


For example If the user scored a 90-100 it informs the user that they scored an A

Q1-11) Write a C++ program to read strings from file1.txt and removes all the letters ‘A’ from those strings
and store it again in the file.

Q1-12) Write a C++ program that represent a simple database for a pharmacy. The program should have a
structure for the medicine which has information items (ID, name, manufacturer, and expiry_date).
Let the user input 3 medicines information and store it in an array of that structure then store in a file.
Use Functions.

Hint:
• Define a structure type called medicine
• Define a global array of that type
• Construct two functions to perform required tasks: Input_MedicineArray() and
Write_MedicineArray_To_File()

Q1-13) Write a C++ program that represent a simple database for a hotel. The program should have a
structure for the room which has information items (room_no, floor, price, and customer_name).
Let the program read 10 rooms information from a file and store it in an array of that structure then show the
array on the screen. Use Functions.

Hint:
• Define a structure type called room
• Define a global array of that type
• Construct two functions to perform required tasks: and Read_RoomArray_From_File() and
Show_RoomArray()
GROUP 2 –Functions
Note: for physical quantities or geometrical dimensions use float or double.

Q2-1\ Write a C++ function that takes as input the mass, M, of an object and returns its weight W.
Note: Weight = Mass * 9.8

Q2-2\ Write a function in C++ that converts kilometers per hour to miles per hour.
Note: 1 mile = 1.60934 km

Q2-3\ Write a C++ function that calculates the volume of the cube taking the side as input.

Q2-4\ Write a C++ function that calculates the volume of the Sphere taking the radius as input.

Q2-5\ Write a C++ function that calculates the volume of the Cylinder taking the radius and height as input.

Q2-6\ Write a C++ function Check_Even that returns true if the input integer is even.
For example: 6 should return true, but 11 should return false.

Q2-7\ Write a C++ to function that finds the third angle of a triangle taking the other two angles as input.
Note: the sum of the angles in a triangle is always 180.

Q2-8\ Write a C++ Function that calculates c depending on the values of a and b in the right angle triangle.
You can use the built-in function sqrt().

Pythagorean Theorem
GROUP 3 – Find Output : Evaluate the output for below:
Q3-1\ Evaluate the output for below
#include <iostream>
using namespace std;

void Cube(double y)
{
y = y*y*y;
}

void main()
{
double g = 4.0;
Cube(g);
cout << g<<endl;
system("pause");
}

Q3-2\
#include <iostream>

int foo(int x)
{
return x+1;
}

int main()
{
int x = 13;
int y = 16;
cout << foo(y) << endl;
return 0;
}

Q3-3\
#include <iostream>
using namespace std;
int main()
{
for (int i = 0; i < 4; ++i)
{
switch (i)
{
case 0: cout << "0";
case 1: cout << "1"; continue;
case 2: cout << "2"; continue;
default: cout << "D"; break;
}
cout << ".";
}
system("pause");
return 0;
}
Q3-4\
#include <iostream>
using namespace std;

void Unit_Array (int* a, int n)


{
for (int j = 0; j < n; j++)
{
a[j] = 1;
}

void main()
{
int H[4] = {10, 20, 30, 40 };
Unit_Array (H, 4);
cout << K[2] << endl;
system("pause");
}
Q3-5\

#include <iostream>
using namespace std;

void foo(int* x, int y)


{
(*x) = (*x)* (*x);
y = y *y;
}

void main()
{
int a = 5, b = 6;
foo(&a, b);
cout << a << " " << b<<endl;
system("pause");
}
Q3-6\
#include <iostream>
using namespace std;

void f(int &a, int b, int &c) {


b = a * c;
a = b - c;
}
int main()
{
int a = 16;
int b = 2;
f(b, a, b);
cout << a << ' ' << b << endl;
system("pause");
return 0;
}
Q3-7\
#include <iostream>
using namespace std;

void bar(int* a)
{
for (int j = 0; j < 4; j++)
{
a[j] = j*100;
}

void main()
{
int a[4] = {10, 20, 30, 40 };
bar(a);
for (int i = 3; i >= 0; i--)
{
cout << a[i] << " ";
}
system("pause");
}

Q3-8\
#include <iostream>
using namespace std;
void swap(int & a, int b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 9;
int b = 5;
int c = 2;
swap(a, b);
swap(b, c);
cout << a << '\n' << b << '\n' << c;
system("pause");
return 0;
}
Samples of Solved Questions from Mid Term Exam
MT-1) Assuming an array who has 7 elements with following initial values:
{0.2, 0.4, 0.95, 0.88, 0.56, 0.56, 0.7}
Write a C++ program to convert these values in the same array to percentages like below
{20, 40, 95, 88, 56, 56, 70}
You must use a function named Conv_Percent that should take the array as input.
#include <iostream>
using namespace std;
void Conv_Percent(double *B)
{
for (int g = 0; g < 7; g++)
{
B[g] = B[g] * 100.0;
}
}
void main()
{
//Define Stage
double A[7]= { 0.2, 0.4, 0.95, 0.88, 0.56, 0.56, 0.7 };
//No Input Stage
//Process Stage
Conv_Percent(A);
//Output stage (optional)
for (int i = 0; i < 7; i++)
{
cout << A[i] << endl;
}
cout << endl;
system("pause");
}
MT-2) Write a C++ program to calculate the sum of following series:
1 + 1/4 + 1/7 + 1/10 + 1/13 +…………+ 1/31
#include <iostream>
using namespace std;
int main()
{
//Define Stage
int i = 1;
double sum= 0;
//No input stage
//Process Stage
for (i = 1; i <= 31; i = i + 3)
{
sum = sum + (1.0 / i);
}
//Output Stage
cout << sum<< endl;
system("pause");
return 0;
}
MT-3)Write a C++ program to draw the shape below. The program should use a function Draw_Squar that
takes the character and the square side length as input

#include <iostream>
using namespace std;

void Draw_Square(char c, int n)


{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << c << " ";
}
cout << endl;
}
}
int main()
{
//Output Stage
Draw_Square('$', 3);
Draw_Square('#', 4);
system("pause");
return 0;
}

MT-4)Write a function in C++ that converts kilometers to miles.


Note: 1 mile = 1.60934 km

double Kms_To_Miles(double kms)


{
return (kms/1.60934);
}

MT-5)Write a C++ to function that finds the third angle of a triangle taking the other two angles as input.
Note: the sum of the angles in a triangle is always 180.
double Third_Angle(double angle1, double angle2)
{
return (180 – angle1 – angle2);
}
MT-6)Write a C++ function Check_Odd that returns true if the input integer is odd.
For example: 7 should return true, but 10 should return false

bool Check_Odd(int n)
{
return (n%2==1);
}

MT-7)Write a program that asks for an integer (1-12) representing the month, and depending on the input,
show the short month name as a word (in English).
Use do while for input validation and then use switch statement.
For example for input 1 it should print Jan
#include <iostream>
using namespace std;
void main()
{ //Define Stage
int num=0;
//Input Stage with vlidation
do
{
cout << "Input the month number (1 - 12) "
<<endl;
cin >> num;
} while (num < 1 || num >12);
//Output stage using switch
cout << endl;
cout << "The month selected is ";
switch(num)
{
case 1: cout << "Jan";break;
case 2: cout << "Feb";break;
case 3: cout << "Mar";break;
case 4: cout << "Apr";break;
case 5: cout << "May";break;
case 6: cout << "Jun";break;
case 7: cout << "Jul";break;
case 8: cout << "Aug";break;
case 9: cout << "Sep";break;
case 10: cout << "Oct";break;
case 11: cout << "Nov";break;
case 12: cout << "Dec";break;
}
cout <<endl;
system("pause");
}

You might also like