0% found this document useful (0 votes)
63 views8 pages

C++ Finals Practice

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

C++ Finals Practice

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

1.

Write a program that prompts the user to enter the year and month, and displays the
number of days in the month.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int days,month,year;
bool leapsum;

cout<<"please enter the number of the month to access the calender"<<endl;

cin>>month>>year;

leapsum=((year%4==0)&&(year%100!=0))||(year%400==0);
switch(month){
case 1:case 3: case 5: case 7: case 8: case 10:case 12:
cout<<"31"<<endl;
break;
case 2: if(leapsum){
cout<<"29";
}
else{
cout<<"28";
}
break;

case 4: case 6: case 9: case 11:


cout<<"30";
break;
}

return 0;
}

2. Write a recursive C++ function to compute 2 + 4 + … + 2n


#include <iostream>

using namespace std;


int multiply(int n){
if(n==2){
return 2;
}
else {
return n+multiply(n-2);
}

int main()
{
int n=6;
cout<<"enter your number"<<endl;
cin>>n;
cout<<" the sum is "<<multiply(n)<<endl;
return 0;
}
3. Write a program to draw a square with a size of 10 using sleep function to draw star by star
every one second.
Answer=
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
for(int row=0;row<10;row++)
{
for (int col=0;col<10;col++)

if(row<11)
{
if (col==0||row==0||row==9||col==9){
cout<<"* ";
Sleep(1000);
}

else {
cout<<" ";
}
}
cout<<endl;
}
return 0;
}

4. Write a program using recursion functions to print backwards. If the user insert @ the
program will stop and print the characters you have been inserted.
Ask the user if he/she wants to play again.

For example.
The program will ask you to insert 1 character : 2
The program will ask you to insert 2 character : R
The program will ask you to insert 3 character : P
The program will ask you to insert 4 character : “space”
The program will ask you to insert 5 character : +
The program will ask you to insert 6 character : +
The program will ask you to insert 7 character : C
The program will ask you to insert 8 character : @
the result: C++ PR2

Answer=

Solution:1

#include <iostream>

#include <cstdio>

using namespace std;


void printspace(int n){

string b;

cout<<"Insert "<<n<<" Character"<<endl;


getline(cin,b);
if(b=="@"){
cout<<"";
}
else{
printspace(n+1);
cout<<b;
}
}
int main()
{
printspace(1);

return 0;

Solution:2 (using concatenation)

#include <iostream>

#include <cstdio>

using namespace std;

string printspace(int n){

string b;

cout<<"Insert "<<n<<" Character"<<endl;


getline(cin,b);
if(b=="@"){
return "";
}
else{
printspace(n+1);
b=printspace(n+1)+b;
}
return b;

}
int main()
{
cout<<printspace(1);
return 0;

5. write a C++ function that takes a 2-dimensional integer array with 10 rows and 5
columns as an input and returns the max value of that array and its location within the
array as reference parameters.

Answer.

#include <iostream>

#include <windows.h>

using namespace std;

int *findMax(int z[2][3], int m, int n){

int maximum=z[0][0];

int *num = new int[3];

for(int x=0;x<m;x++){

for(int i=0;i<n;i++){

if(z[x][i]>maximum){

maximum=z[x][i];

num[0]=maximum;

num[1]=x;

num[2]=i;

}
return num;

int main()

int student[2][3];

for(int x=0;x<2;x++){

for(int i=0;i<3;i++){

cout<<"Enter next number"<<endl;

cin>>student[x][i];

int *maximum=findMax(student,2,3);

cout<<"Maximum value is "<<maximum[0]<<endl;

cout<<"The position is ("<<maximum[1]<<","<<maximum[2]<<")";

// cout<<"Maximum value is"<<maximum[0];

return 0;

6. create Student class. The class has these attributes: id,


name and faculty. The class has method of register which receive id, name
and faculty parameters. Declare Student class and define the methods
main cpp.
#include <iostream>
#include "student.h"
using namespace std;
int main()
{
Student a;
cout<<a.getName()<<endl;
Student b;
b.setName("Shakil");
b.setID(110046445);
b.setFaculty("BSEM");
cout<<b.getName()<<endl;
cout<<b.getID()<<endl;
cout<<b.getFaculty()<<endl;
return 0;
}

Student.h
#include <iostream>
#include <string>

using namespace std;


class Student
{
private:
string Name;
int ID;
string Faculty;

public:
Student();
Student(string,int,string);
string getName();
void setName (string);
int getID();
void setID (int);
string getFaculty();
void setFaculty(string);
};

Student.cpp
#include "Student.h"

Student::Student(){

Name="hamza";
ID=110046852;
Faculty="FICT";

}
Student::Student(string a,int b,string c){
Name=a;
ID=b;
Faculty=c;

};
string Student::getName(){
return Name;
}
void Student::setName(string a){
Name=a;
}

int Student::getID(){
return ID;
}
void Student::setID(int b){
ID=b;
}

string Student::getFaculty(){
return Faculty;
}
void Student::setFaculty(string c){
Faculty=c;
}

You might also like