Lab 2

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

North Border University

Turaif science and Literature University


First term (1442-1443)
Department of Computers and Information technology
Data Structure
Instructor: Dr. Salwa Othmen

Lab2 : Array and String

‫ بندر نايف الشمري‬/ ‫الاسم‬


201602993‫الرقم الجامعي‬/

Exercise 1: Multi-dimensional array

1. Write a C++ program to create a multi-


dimensional Array named student: (4,5)
2. Initiate this array from the keyboard
3. Print the content of the array
4. Find the sum of the array elements
5. Calculate the average of the array elements

#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

1. Take a first name and a last name and combines


the two strings.

#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.";

You might also like