Sei sulla pagina 1di 9

ARRAY[2]

LAB # 8

Spring 2019
CSE102L Computer Programming Lab

Submitted by:Muhammad Umar Khan


Registration No. : 18pwcse1663
Class Section: B

“On my honor, as student of University of Engineering and Technology, I have neither given nor
received unauthorized assistance on this academic work.”

Student Signature: ______________

Submitted to:
Engr. Madiha Sher
Month Day, Year (e.g. February 17, 2018)

Department of Computer Systems Engineering


University of Engineering and Technology, Peshawar
Task No 1

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

int main()
{
string str_to_search, str;
char str1[1000], str2[100];
cout << "Please enter the string to search : "<<endl;
str_to_search = (cin.getline(str1, sizeof(str1)), str1);
cout << "Enter the substring to find : ";
str = (cin.getline(str2, sizeof(str2)), str2)
int count = 0;
unsigned int nPos = 0;
while((unsigned int)(nPos = str_to_search.find(str, nPos)) != (unsigned int)std::string::npos)
{
count++; nPos += str.size();
}
cout << "Found " << count << " substring(s) in the string" << endl;
return 0;
}

Task:#2

Title:

Take a set of N students’ examination marks (in the range 0 to 100) as input, Formulate a
program that makes a count of the number of students that passed and failed the examination
separately. A pass is awarded for all marks of 50 and above. Also display the minimum,
maximum and average score.
Code for program:

#include <iostream>

using namespace std;

int main()
{
int student_Marks[10];
int i=0,j,mini,maxi;
int sum_pass=0,sum_fail=0;

for (i=0;i<10;i++){
cout<<"student "<<i+1<<" Marks:";
cin>>student_Marks[i];
for (j=0;j<10;j++){
if(student_Marks[i]>100){
cout<<"Invalid Entry"<<endl;
cout<<"Enter Again for student "<<i+1<<":";
cin>>student_Marks[i];
}
if(student_Marks[i]>=50){
sum_pass++;
}
else if(student_Marks[i]<50){
sum_fail++;
}
}
}
cout<<"\n\n==>>>==>>pass students:"<<sum_pass/10<<endl;
cout<<"\n\n==>>>==>>fail students:"<<sum_fail/10<<endl;
maxi=student_Marks[0];
for(i=1;i<10;i++){
if(maxi<student_Marks[i]){
maxi=student_Marks[i];
}
}
cout<<"\n\n==>>>==>>Max marks:"<<maxi<<endl;;

mini=student_Marks[0];
for(i=1;i<10;i++){
if(mini>student_Marks[i]){
mini=student_Marks[i];
}
}
cout<<"\n\n==>>>==>>MIN marks:"<<mini<<endl;
return 0;
}
Task: #3
Title:
1. Write a program that uses functions to perform the following operations:
a) Insert sub-string into main string from given position.
b) Delete n Characters from a given position in given string.
Sample Output:

Code for program:


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

int main(){
string u;
cout<<"Enter a First String"<<endl;
cin>>u;
string s;
cout<<"Enter a second String"<<endl;
cin>>s;
cout<<u.insert(3,s);
return 0;
}

Task: #4
Title:
Write a program that takes two arrays as input and find the sum of all the elements of both the
arrays and displays which array has the higher sum value.

Code for Program:

#include <iostream>

using namespace std;

int main()
{
int Arr1[5];
int sum_of_Arr1=0,sum_of_Arr2=0;
cout<<"Enter Array No:1"<<endl;
for(int i=0;i<5;i++){
cin>>Arr1[i];
sum_of_Arr1=sum_of_Arr1+Arr1[i];
}
cout<<"Enter Array No:2"<<endl;
int Arr2[5];
for(int j=0;j<5;j++){
cin>>Arr2[j];
sum_of_Arr2=sum_of_Arr2+Arr2[j];
}
cout<<"Sum of Array No:1 elements:"<<sum_of_Arr1<<endl;
cout<<"Sum of Array No:2 elements:"<<sum_of_Arr2<<endl;
if(sum_of_Arr1>sum_of_Arr2){
cout<<"\n\nSUM of Element of Array No:1 is Higher"<<endl;
}
else {
cout<<"\n\nSUM of Element of Array No:2 is Higher"<<endl;
}
return 0;
}

Task: #5
Title
Write a program that takes a string as input. If the character is upper case convert it into lower
case and if the character is lower case then convert it in to upper case.
Code for Program:

// CPP program to Convert characters


// of a string to opposite case
#include<iostream>

using namespace std;

int main()
{
string str;
cout<<"Enter To change Interchange its capital and small letters"<<endl;
getline(cin,str);
// Calling the Function
int ln = str.length();

// Conversion according to ASCII values


for (int i=0; i<ln; i++)
{
if (str[i]>='a' && str[i]<='z')
//Convert lowercase to uppercase
str[i] = str[i] - 32;
else if(str[i]>='A' && str[i]<='Z')
//Convert uppercase to lowercase
str[i] = str[i] + 32;
}

cout << str;


return 0;
}

Potrebbero piacerti anche