0% found this document useful (0 votes)
15 views7 pages

Lab 2

This document contains a lab exercise on arrays and strings in C++. It includes 3 exercises: 1) Create a multi-dimensional array to store student marks and perform operations like inputting data, printing, calculating sum and average. 2) Combine two strings for first and last name into a full name and append additional strings. Also print the length of the strings. 3) Compare two input strings and check if they are equal or not equal.
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)
15 views7 pages

Lab 2

This document contains a lab exercise on arrays and strings in C++. It includes 3 exercises: 1) Create a multi-dimensional array to store student marks and perform operations like inputting data, printing, calculating sum and average. 2) Combine two strings for first and last name into a full name and append additional strings. Also print the length of the strings. 3) Compare two input strings and check if they are equal or not equal.
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

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 = [Link]();
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