0% found this document useful (0 votes)
11 views11 pages

Chp 5 lab activities

The document contains multiple C++ programs that perform various tasks such as reading and printing numbers in reverse order, finding the smallest number and its index, counting occurrences of a specific number, displaying elements of a two-dimensional array, summing positive numbers, adding two arrays, inputting and processing a two-dimensional array, copying and concatenating strings, and identifying the smallest of three strings. Each program is clearly defined with input prompts and output statements. The code snippets demonstrate fundamental programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

zafarmahmood547
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)
11 views11 pages

Chp 5 lab activities

The document contains multiple C++ programs that perform various tasks such as reading and printing numbers in reverse order, finding the smallest number and its index, counting occurrences of a specific number, displaying elements of a two-dimensional array, summing positive numbers, adding two arrays, inputting and processing a two-dimensional array, copying and concatenating strings, and identifying the smallest of three strings. Each program is clearly defined with input prompts and output statements. The code snippets demonstrate fundamental programming concepts such as loops, conditionals, and array manipulation.

Uploaded by

zafarmahmood547
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/ 11

ii. Write a program that reads ten numbers in an array and print them in reverse order.

#include<iostream>
using namespace std;
int main()
{
int arr[10], i;
cout<<"Enter 10 Array Elements: ";
for(i=0; i<10; i++)
cin>>arr[i];

cout<<"\n\nThe Reverse of Given Array is:\n";


for(i=(10-1); i>=0; i--)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
iii. Write a program that reads ten numbers and print the smallest along with its index.
#include <iostream>
using namespace std;

int main(){
int input[5], count, i, min,index;
cout<<"Enter ten numbers: ";
// Read array elements
for(i = 0; i < 10; i++){
cin >> input[i];
}

min = input[0];
// search num in inputArray from index 0 to elementCount-1
for(i = 0; i < 10; i++){
if(input[i] < min){
min = input[i];
index=i;
}
}

cout << "Minimum Number\n" << min<<endl;


cout << "Index of smalles Number is: "<<index;
return 0;
}
iv. For the given array,
Write a program that prints the number of times the number 5 appears in the array.
#include <iostream>
using namespace std;
int main(){
int count=0;
int array[15]={4,8,5,1,3,5,0,12,5,7,3,15,8,4,11};

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


if (array[i] == 5) {
count++;
}
}
cout<<"The number 5 appears " << count <<" times in the array."<<endl;
return 0;
}
v. For the given array:
Write a program that displays all the elements in the form of…. ?
#include<iostream>
using namespace std;
int main()
{
int arr[3][2] = {{6,3}, {7,8}, {4,5}};
int i, j, totalSum=0;
cout<<"The Two-dimensional Array is:\n";
for(i=0; i<3; i++)
{
for(j=0; j<2; j++){
cout<<arr[i][j]<<" ";
totalSum=totalSum+arr[i][j];
}

cout<<endl;
}
cout<<endl;
cout<<"Sum is: "<<totalSum;
return 0;
}

vi. For the given array, Write a program to find the sum of positive numbers.
#include<iostream>
using namespace std;
int main()
{
int sum=0;
int array[3][4]={{4,18,-16,11},{-5,10,-2,12},{15,-3,17,18}};
for(int i=0; i<3; i++)
{
for(int j=0;j<4;j++){
cout<<array[i][j]<<" ";
if(array[i][j]>0){
sum=sum+array[i][j];
}
}
cout<<endl;
}
cout<<endl;
cout<<"Sum of Positive numbers: "<<sum;
return 0;
}

vii. For the given array, Write a program that adds the two arrays and produce.. ?
#include<iostream>
using namespace std;
int main()
{
int a[2][4]={{14,8,11,10},{15,12,20,3}};
int b[2][4]={{2,3,4,7},{6,7,8,9}};
for(int i=0;i<2;i++){
for(int j=0;j<4;j++){
cout<<a[i][j]+b[i][j]<<" ";
}
cout<<endl;
}
return 0;
}
viii. Input data from keyboard in a two dimensional array x, that has r rows and c columns .. ?
#include <iostream>
using namespace std;
int main()
{
int size=2;
int a[size][size];
int row, col, sum;
cout<<"Please Enter elements in array of size "<<size<<"x"<<size<<endl<<endl;
for(row=0; row<size; row++)
{
for(col=0; col<size; col++)
{
cin>>a[row][col];
}
}
for(row=0; row<size; row++)
{
for(col=0; col<size; col++)
{
cout<<a[row][col]<<" ";
}
cout<<endl;
}
for(row=0; row<size; row++)
{
sum = 0;
for(col=0; col<size; col++)
{
sum = sum + a[row][col];
}
cout<<"Sum of elements of Row: "<< row+1<<" is "<< sum<<endl;
}
for(col=0; col<size; col++)
{
sum = 0;
for(row=0; row<size; row++)
{
sum += a[row][col];
}
cout<<"Sum of elements of Column: " <<row+1<<" is "<<sum<<endl;
}
}
ix. Write a program that reads a string, copies into another string and then print both..?
#include <iostream>
using namespace std;
int main()
{
string a="This is a string.";
string b=a;
cout<<"First String: "<<endl<<a<<endl;
cout<<"Copied String: "<<endl<<b<<endl;
}

x. Write a program that reads 2 strings of size … ?


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

int main()
{
char s1[20], s2[20];

cout << "Enter Sring 1: ";


cin.getline(s1, 20);
cout << "Enter Sring 2: ";
cin.getline(s2, 20);

cout<<"String 1: "<<s1<<endl;
cout<<"String 2: "<<s2<<endl;

strcat(s1, s2);

cout << "Concatenation: " << s1 << endl;

return 0;
}

xi. Write a program that reads 3 strings and prints the smallest.
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char str1[80],str2[80],str3[80];
cout<<"Enter three strings: "<<endl;
cin.getline(str1,80);
cin.getline(str2,80);
cin.getline(str3,80);

if(strlen(str1)<strlen(str2)&&strlen(str1)<strlen(str3))
cout<<"String 1 is the smallest";
else if(strlen(str2)<strlen(str3)&&strlen(str2)<strlen(str1))
cout<<"String 2 is the smallest";
else if(strlen(str3)<strlen(str2)&&strlen(str3)<strlen(str1))
cout<<"String 3 is the smallest";

return 0;

You might also like