C++ Programming Assignment A.
Rehman | 25es014
C++ Programming Assignment
Computer Programming Assignment
Student Name [Link]
Roll No 25es028
Submitted to Sir Rizwan Baloch
Programs Included
1. Online Quiz System: Control Structures & Functions
2. Ohm's Law Function: User-Defined Functions & Arithmetic
3. Star Pyramid Pattern: Nested Loops & Pattern Logic
Page 1 of 7
C++ Programming Assignment [Link] | 25es014
1. Online Quiz System
Problem Statement
Design and implement a simple Online Quiz System in C++ that presents the user with multiple-
choice questions, evaluates their answers using conditional logic, keeps track of the total score, and
displays a result message at the end using a separate function.
Objective
Develop a simple online quiz system using control structures and functions.
Source Code
#include <iostream>
using namespace std;
void showResult(int score) {
cout << "\nFinal Score = " << score << endl;
if(score >= 3) cout << "Congratulations! You Passed.\n";
else cout << "Better Luck Next Time.\n";
}
int main() {
int choice, score = 0;
cout << "===== ONLINE QUIZ SYSTEM =====\n";
while(true) {
cout << "\n1. What is the capital of Pakistan?\n";
cout << "1. Lahore\n2. Karachi\n3. Islamabad\n";
cin >> choice;
if(choice == 3) { score++; cout << "Correct!\n"; }
else cout << "Wrong Answer!\n";
cout << "\n2. C++ is a:\n";
cout << "1. Programming Language\n2. Browser\n3. Game\n";
cin >> choice;
if(choice == 1) { score++; cout << "Correct!\n"; }
else cout << "Wrong Answer!\n";
cout << "\n3. 5 + 5 = ?\n";
cout << "1. 8\n2. 10\n3. 15\n";
cin >> choice;
if(choice == 2) { score++; cout << "Correct!\n"; }
else cout << "Wrong Answer!\n";
break;
}
showResult(score);
return 0;
}
Page 2 of 7
C++ Programming Assignment [Link] | 25es014
Code Explanation
The program is structured into two main parts: a helper function and the main logic loop.
Component Description
#include <iostream> Includes the standard I/O library for cin and cout operations.
showResult(int score) A void function that receives the final score, prints it, and displays a
pass/fail message based on whether the score is >= 3.
int choice, score = 0 Declares two integer variables: 'choice' stores the user's answer input,
'score' tracks correct answers starting at 0.
while(true) { ... A while loop that always runs once — acting as a structured block. The
break; } 'break' at the end ensures it runs exactly one iteration (one full quiz
session).
if(choice == 3) Checks if the user's input matches the correct answer. If correct,
score++ increments the score counter by 1.
showResult(score) Calls the function after all 3 questions, passing the accumulated score for
final display.
Execution Result
===== ONLINE QUIZ SYSTEM =====
1. What is the capital of Pakistan?
1. Lahore
2. Karachi
3. Islamabad
3
Correct!
2. C++ is a:
1. Programming Language
2. Browser
3. Game
1
Correct!
3. 5 + 5 = ?
1. 8
2. 10
3. 15
2
Correct!
Final Score = 3
Congratulations! You Passed.
=== Code Execution Successful ===
Page 3 of 7
C++ Programming Assignment [Link] | 25es014
2. Ohm's Law Function
Problem Statement
Write a C++ program that defines a function named 'ohm' to calculate the electric current flowing
through a circuit. The function should accept voltage (V) and resistance (R) as parameters and
return the current (I) using Ohm's Law: I = V / R.
Objective
Create a function 'ohm' to calculate current (I = V / R).
Source Code
#include <iostream>
using namespace std;
float ohm(float voltage, float resistance) {
return voltage / resistance;
}
int main() {
float V, R, I;
cout << "Enter Voltage: ";
cin >> V;
cout << "Enter Resistance: ";
cin >> R;
I = ohm(V, R);
cout << "Current = " << I << " Ampere";
return 0;
}
Code Explanation
This program demonstrates the use of user-defined functions with parameters and return values,
along with basic floating-point arithmetic.
Component Description
float ohm(...) A user-defined function that takes two float parameters (voltage and
resistance) and returns their quotient as a float — the calculated current.
return voltage / Implements Ohm's Law directly: divides voltage by resistance and returns
resistance the result to the calling function.
float V, R, I Three floating-point variables declared in main to store voltage input,
resistance input, and the computed current respectively.
Page 4 of 7
C++ Programming Assignment [Link] | 25es014
cin >> V / cin >> R Reads user-supplied voltage and resistance values from standard input at
runtime.
I = ohm(V, R) Calls the ohm function, passing user inputs as arguments. The returned
current value is stored in variable I.
cout << I << " Outputs the computed current value followed by the unit label to the
Ampere" console.
Execution Result
Enter Voltage: 25
Enter Resistance: 44
Current = 0.568182 Ampere
=== Code Execution Successful ===
Page 5 of 7
C++ Programming Assignment [Link] | 25es014
3. Star Pyramid Pattern
Problem Statement
Write a C++ program that uses nested for loops to print a symmetrical diamond-shaped star pattern
on the console. The pattern should expand from the top apex to a widest middle row, then mirror
itself back to a single star at the bottom.
Objective
Use nested loops to print a symmetrical diamond star pattern.
Source Code
#include <iostream>
using namespace std;
int main() {
int rows = 6;
for(int i = 1; i <= rows; i++) {
for(int j = i; j < rows; j++) cout << " ";
for(int k = 1; k <= (2*i - 1); k++) cout << "* ";
cout << endl;
}
for(int i = rows - 1; i >= 1; i--) {
for(int j = rows; j > i; j--) cout << " ";
for(int k = 1; k <= (2*i - 1); k++) cout << "* ";
cout << endl;
}
return 0;
}
Code Explanation
The diamond pattern is drawn in two halves using two separate outer for loops, each containing two
inner loops for spacing and stars.
Component Description
int rows = 6 Sets the number of rows for the upper half of the diamond. The total
diamond height will be (2 * rows - 1) = 11 rows.
Outer loop (upper Runs from i=1 to i=rows. Each iteration i represents one row of the upper
half) pyramid, with the star count growing as i increases.
Spacing loop (j) Prints leading spaces before the stars. As i increases, fewer spaces are
printed, shifting stars toward the center.
Page 6 of 7
C++ Programming Assignment [Link] | 25es014
Star loop (2*i - 1) Prints the correct number of stars per row using the formula 2*i-1. For row
1: 1 star, row 2: 3 stars, row 3: 5 stars, and so on.
Outer loop (lower Runs from i=rows-1 down to i=1, mirroring the upper half. Star count
half) decreases symmetrically as the pattern narrows back.
cout << endl Moves the cursor to the next line after each row is printed, producing the
stacked visual pattern.
Execution Result
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
=== Code Execution Successful ===
Page 7 of 7