Chp 5 lab activities
Chp 5 lab activities
#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];
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<<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;
}
int main()
{
char s1[20], s2[20];
cout<<"String 1: "<<s1<<endl;
cout<<"String 2: "<<s2<<endl;
strcat(s1, s2);
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;