Professional Documents
Culture Documents
Lab 2
Lab 2
Lab 2
#include <iostream>
using namespace std;
int main()
{
double marks[4][5] = {{85,75,90,80,70},
{95,70,77,89,95}, {87,80,88,90,83}, {65,45,60,50,60}}
;//2d array to store marks
string
names[4]={"Maxwell","Carl","Gerhard","Paul"}; /
/1d array to store names
int num;
double total,avg;
cout<<"Enter a number (1 to 5)";
cin>>num;
do{
cout<<"\nStudent Name: "<<names[num-1];
//displaying students name
cout<<"\nJD521: "<<marks[num-1][0];
//displaying marks in JD521
cout<<"\nPRG521: "<<marks[num-1][1];
//displaying marks in PRG521
cout<<"\nIPG521: "<<marks[num-1][2];
//displaying marks in IPG521
cout<<"\nCPG521: "<<marks[num-1][3];
cout<<"\nRIPG521: "<<marks[num-1][4];
total=marks[num-1][0]+marks[num-1]
[1]+marks[num-1][2]+marks[num-1]
[3]+marks[num-1][4]; //calculate total marks
avg=total/5; //calculate average of marks
cout<<"\nTotal of marks: "<<total; //displaying
total marks
cout<<"\nAverage of all marks: "<<avg;
//displaying average of 3 marks
if(avg>70) //if average is greater than 70, student
passed
cout<<"\nStudent has passed";
else //if average is less than 70, student failed
cout<<"\nStudent has not passed\n";
cout<<"\nEnter a number (1 to 5)";
cin>>num;
}while(num<=5); //loop continues till user enter
num greater than 6
return 0;
}
Exercise 2: String
#include <string>
#include <iostream>
using namespace std;
int main() {
string firstname = "Bander";
string lastname = "Nayef";
string fullname = firstname + lastname; // concat the two
strings
fullname += ",Al-Shamri"; // append another string
fullname += '.'; // append a single char
cout << firstname << lastname << endl;
cout << fullname << endl;
return 0;
}
2. Print the length of these two strings
#include<iostream>
#include<string>
using namespace std;
int main()
{
string txt;
cout << "\nPlease Enter the String to find Length =
";
getline(cin, txt);
int len = txt.length();
cout<< "\nThe Length of '" << txt << "' String = " <<
len;
return 0;
}
3. Compare these two strings
#include<iostream>
using namespace std;
#include<string.h>
int main() {
const char *str_inp1="Bandar";
const char *str_inp2="bander";
cout<<"String 1:"<<str_inp1<<endl;
cout<<"String 2:"<<str_inp2<<endl;
if (strcmp(str_inp1, str_inp2) == 0)
cout << "\nBoth the input strings are equal." <<
endl;
else
cout << "The input strings are not equal.";