0% found this document useful (0 votes)
80 views27 pages

20-Cs-55 Final PF Lab

The document contains the final exam questions and code solutions for a C++ programming lab. It includes 5 questions: 1. A program to print a pattern using loops based on user input characters and range. 2. Converting all for loops to while loops to produce the same output. 3. A function that adds arrays and returns the result. 4. A program to evaluate football player performance and rewards based on sorting and conditions. 5. A driver program to execute the above questions based on user choice.

Uploaded by

Olde Gamerz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views27 pages

20-Cs-55 Final PF Lab

The document contains the final exam questions and code solutions for a C++ programming lab. It includes 5 questions: 1. A program to print a pattern using loops based on user input characters and range. 2. Converting all for loops to while loops to produce the same output. 3. A function that adds arrays and returns the result. 4. A program to evaluate football player performance and rewards based on sorting and conditions. 5. A driver program to execute the above questions based on user choice.

Uploaded by

Olde Gamerz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

NAME: SYEDA KHADIJA NADEEM

REG NO: 20-CS-55


FINAL EXAM
PF LAB

Question-1
Write a program that inputs two characters from the user and a range value.
It then displays a shape as per the following pattern depending upon the range
value entered by the user using loop. i.e. it will display,
*****%*****
****%*%****
***%***%***
**%*****%**
*%*******%*
if user enters “*” & “%”as input and range as 5. (Minimum range value to be
taken= 6)

CODE:
#include <iostream>
using namespace std;

int main() {
int l = 0;
cout << "Please Enter number of lines : ";
cin >> l;
if (l < 6)
cout << "Minimum number of lines are 6. Please! Enter again.";
else {
char ch1, ch2;
cout << "Enter Char 1 : ";
cin >> ch1;
cout << "Enter Char 2 : ";
cin >> ch2;
int Stars = 1;
for (int x = 0; x < l; x++) {
for (int y = l; y > x; y--) cout << ch1;
cout << ch2;
for (int z = 0; z < Stars && x != 0; z++) cout << ch1;
if (x != 0) {
cout << ch2;
Stars += 2;
}
for (int i = l; i > x; i--) cout << ch1;
cout << endl;
}
}
return -1;
}
Question-2
Consider Figure-A. You will get output by running this code. Achieve
that same output by converting all for loop into While
loop(completely)
CODE:
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
int i,j;
int A[5]={58,21,64,17,79};
i=0;
while(i<5)
{int j=j+1;
while(j<5)
{if(A[j]>A[i]){
A[i]+=A[j];
A[j]=A[i]-A[j];
A[i]=A[i]-A[j];
}
j++;
}
i++;
}
i=0;
while(i<5){
cout<<A[i]<<"";
i++;
}
i=0;
while(i<5)
{
j=i+1;
while(j<5)
{
if(A[j]<A[i])
{A[j]+=A[i];
A[i]=A[j]-A[i];
A[j]=A[j]-A[i];
}
j++;
}
i++;
}
i=0;
while(i<5){
cout<<A[i]<<"";
i++;}
system("pause");
return 0;
}

Question-3
Write a program that inputs five integers in two arrays each. It declares a
function that accepts four parameters. The first parameter is the first array, the
second parameters the second array, the third parameter is the third array and
the fourth parameter is the length of the arrays. The function adds the
corresponding values of the first two arrays and stores the result in The
corresponding element of the third array. The main function finally displays the
values of all arrays. (Length of all arrays must be same)

CODE:

#include<iostream>
using namespace std;
int * fun(int array1[],int array2[],int array3[],int length){
for(int k=0;k<length;k++){
array3[k]=array1[k]+array2[k];
}
return array3;
}
int main(){
int length;
cout<<"Enter length of an Array: ";
cin>>length;
int array1[length];
for(int k=0;k<length;k++){
cout<<"Enter "<<k+1<<" element of array 1: ";
cin>>array1[k];
}
cout<<"----------------------------\n";
int array2[length];
for(int k=0;k<length;k++){
cout<<"Enter "<<k+1<<" element of array 2: ";
cin>>array2[k];
}
int array3[length];
int *result;
result=fun(array1,array2,array3,length);
cout<<"array1 array2 array3\n";
for(int k=0;k<length;k++){
cout<<array1[k]<<" "<<array2[k]<<" "<<result[k];
cout<<endl;
}
}

Question-4
A Football Club X wants to evaluate the performance of its players and give
rewards/incentives to the best player. Your program will do the following :-Take
the input for no. of players from the user and then stores the information for
them.-Input information as Name, Joining date, Salary, No. of goals scored in
the current league etc.-If a user enters even no of players then it will do
nothing otherwise arrange the players alphabetically and display them.-Now
arrange them based on No. of goals scored in the league.-If the leader board has
a greater salary than others, the club will give a 10% salary bonus and impose
a4.33 % tax overall-If its salary is not greater than other players, the club will
give a 20% salary bonus and impose a 7% tax.-Display all player's data After
appraisal.
CODE:
#include<iostream>
#include <iomanip>
#include <string>
#include <algorithm>
using namespace std;
struct Game {
char name[20];
string join_Date;
int sal;
int goals;
};
void sortStructByName( Game arr[],int size){
bool swapped=true;
do{
swapped=false;
for(int count=0; count<size-1;count++){
if(arr[count].name[0]>arr[count+1].name[0]){
swap(arr[count],arr[count+1]);
swapped=true;
}
}
}while(swapped);
}
void sortStructByGoals( Game arr[],int size){
bool swapped=true;
do{
swapped=false;
for(int count=0; count<size-1;count++){
if(arr[count].goals<arr[count+1].goals){
swap(arr[count],arr[count+1]);
swapped=true;
}
}
}while(swapped);
}
void showStruct( Game x[],int size){
cout << " Name joiningdate salary goals\n";
for(int i=0;i<size;i++){
cout << setw(10) << x[i].name;
cout << setw(10) << x[i].join_Date;
cout << setw(10) << x[i].sal;
cout << setw(10) << x[i].goals<<endl;
}
}
int main(){
int no_of_players;
cout<<"Enter number of players: ";
cin>>no_of_players;
Game x[no_of_players];
for(int i=0;i<no_of_players;i++){

cout<<"Enter "<<i+1<<" name: ";


cin>>x[i].name;
cout<<"Enter "<<i+1<<" joining date: ";
cin.ignore();
getline(cin, x[i].join_Date);
cout<<"Enter "<<i+1<<" salary: ";
cin>>x[i].sal;
cout<<"Enter "<<i+1<<" goals: ";
cin>>x[i].goals;
cout<<"-----------\n";
}
if ( no_of_players % 2 != 0){
sortStructByName(x,no_of_players);
cout << "Here is the players data:\n\n";
showStruct(x,no_of_players);
}

sortStructByGoals(x,no_of_players);
bool check;
for(int i=1; i<no_of_players;i++){
if(x[0].sal>x[i].sal){
check=true;
}else{
check=false;
break;
}
}
if(check){
int sal=x[0].sal;
sal=((sal*10)/100)+sal;
x[0].sal=sal-((sal*4.33)/100);
}else{
int sal=x[0].sal;
sal=((sal*20)/100)+sal;
x[0].sal=sal-((sal*7)/100);
}
cout << "list of the players data after adding bonus and impose tax:\n\n";
showStruct(x,no_of_players);

}
Question-5
Write a program that executes (Question-1 to Question-4) as per
choice entered by the user and keeps on giving that choice to the user
to execute until the user wants to terminate by pressing E.

CODE:
#include<iostream>
#include <iomanip>
#include <string>
#include <algorithm>

using namespace std;


//Question 3 related objects

int * fun(int array1[],int array2[],int array3[],int length)


{
for(int i=0;i<length;i++)
{
array3[i]=array1[i]+array2[i];
}
return array3;
}
//Question 4 related objects
struct Game {
char name[20];
string join_Date;
int sal;
int goals;
};
void sortStructByName(Game arr[],int size){
bool swapped=true;
do{
swapped=false;
for(int count=0; count<size-1;count++){
if(arr[count].name[0]>arr[count+1].name[0]){
swap(arr[count],arr[count+1]);
swapped=true;
}
}
}while(swapped);
}
void sortStructByGoals(Game arr[],int size){
bool swapped=true;
do{
swapped=false;
for(int count=0; count<size-1;count++){
if(arr[count].goals<arr[count+1].goals){
swap(arr[count],arr[count+1]);
swapped=true;
}
}
}while(swapped);
}
void showStruct(Game x[],int size){
cout << " Name joiningdate salary goals\n";
for(int i=0;i<size;i++){
cout << setw(10) << x[i].name<<" ";
cout << setw(10) << x[i].join_Date<<" ";
cout << setw(10) << x[i].sal<<" ";
cout << setw(10) << x[i].goals<<endl;
}
}

class questions
{
int n;
};
void question1();
void question2();
void question3();
void question4();

void question1()
{
int rows = 0;
cout << "Enter no of Rows you want to print : ";
cin >> rows;
if (rows < 6)
cout << "Number of rows must be greater than or equal to 6";
else
{
char s1, s2;
cout << "Enter First Character : ";
cin >> s1;
cout << "Enter 2nd Character : ";
cin >> s2;
int innerChar = 1;
for (int i = 0; i < rows; i++)
{
for (int j = rows; j > i; j--)
cout << s1;
cout << s2;
for (int x = 0; x < innerChar && i != 0; x++)
cout << s1;
if (i != 0) {
cout << s2;
innerChar += 2;
}
for (int y = rows; y> i; y--)
cout << s1;
cout << endl;
}
}

void question2()
{
int i,j;
int A[5]={58,21,64,17,79};
i=0;
while(i<5)
{int j=j+1;
while(j<5)
{if(A[j]>A[i]){
A[i]+=A[j];
A[j]=A[i]-A[j];
A[i]=A[i]-A[j];
}
j++;
}
i++;
}
i=0;
while(i<5){
cout<<A[i]<<" ";
i++;
}
i=0;
while(i<5)
{
j=i+1;
while(j<5)
{
if(A[j]<A[i])
{A[j]+=A[i];
A[i]=A[j]-A[i];
A[j]=A[j]-A[i];
}
j++;
}
i++;
}
i=0;
while(i<5){
cout<<A[i]<<" ";
i++;}
system("pause");
}

void question3()
{

int length;
cout<<"Enter length of array: ";
cin>>length;
int arr1[length];
for(int i=0;i<length;i++){
cout<<"Enter "<<i+1<<" element of array 1: ";
cin>>arr1[i];
}
cout<<"***********\n";
int arr2[length];
for(int i=0;i<length;i++){
cout<<"Enter "<<i+1<<" element of array 2: ";
cin>>arr2[i];
}
int arr3[length];
int *result;
result=fun(arr1,arr2,arr3,length);
cout<<"Array1 Array2 Array3\n";
for(int i=0;i<length;i++){
cout<<arr1[i]<<" "<<arr2[i]<<" "<<result[i];
cout<<endl;
}

}
void question4()
{

int no_of_players;
cout<<"Enter number of players you want to enter: ";
cin>>no_of_players;
Game x[no_of_players];
for(int i=0;i<no_of_players;i++){

cout<<"Enter "<<i+1<<" name: ";


cin>>x[i].name;
cout<<"Enter "<<i+1<<" date of joining : ";
cin.ignore();
getline(cin, x[i].join_Date);
cout<<"Enter "<<i+1<<" salary of player: ";
cin>>x[i].sal;
cout<<"Enter "<<i+1<<" goals by player: ";
cin>>x[i].goals;
cout<<"-----------\n";
}
if ( no_of_players % 2 != 0){
sortStructByName(x,no_of_players);
cout << "Here is the data of the players:\n\n";
showStruct(x,no_of_players);
}

sortStructByGoals(x,no_of_players);
bool check;
for(int i=1; i<no_of_players;i++){
if(x[0].sal>x[i].sal){
check=true;
}else{
check=false;
break;
}
}
if(check){
int sal=x[0].sal;
sal=((sal*10)/100)+sal;
x[0].sal=sal-((sal*4.33)/100);
}else{
int sal=x[0].sal;
sal=((sal*20)/100)+sal;
x[0].sal=sal-((sal*7)/100);
}
cout << "Here is the players data after adding bonus and imposing tax:\n\n";
showStruct(x,no_of_players);

int main()
{
char ch;
int n;
Menu:
cout<<"\n\n\t Please Enter Your Choice : ";
cout<<"\n\n\t";
cout<<"\n A. Question1";
cout<<"\n B. Question2";
cout<<"\n C. Question3";
cout<<"\n D. Question 4";
cout<<"\n E. Exit \n\n\t\t ";

cin>>ch;
system("cls");

switch(ch)
{
case '1':
question1();
goto operror;
break;
case '2':
question2();
goto operror;
break;
case '3':
question3();
goto operror;
break;
case '4':
question4();
goto operror;
break;
case 'E':
goto Exit;;

default: cout<<" Please Enter a valid option";


}
operror:
{
char opselection;
cout<<" 1.Press A to go to menu to perform another execution\n\n 2.Press E to Exit
from menu\n\n\t\t";
cin>>opselection;
if(opselection=='A'||opselection=='a')
{
goto Menu;
}
else if(opselection=='E'||opselection=='e')
{
goto Exit;
}
else
{
cout<<"Please select a valid option";
}
system("cls");

}
Exit:
cout<<"\n\n\n\n\nGood Bye Have a Great Time!\n\n\n\n";

return 0;
}

You might also like