17
Question 1 //1d array #include<iostream.h> #include<conio.h> #include<process.h> int sum_avg() { int n, count; float x, sum, avg; sum = 0; cout << "Array size ? "; cin >> n; int size= n; int array[size]; for (count=1; count<=n; count++){ cout << "Enter Number: "; cin >> array[n]; sum = sum + array[n]; } cout << "The sum is " << sum << endl; avg = sum / n; cout << "The average is " << avg << endl; getch(); return 0;

Computer Assignment

Embed Size (px)

DESCRIPTION

Computer Assignment in C++

Citation preview

Page 1: Computer Assignment

Question 1

//1d array

#include<iostream.h>

#include<conio.h>

#include<process.h>

int sum_avg()

{

int n, count;

float x, sum, avg;

sum = 0;

cout << "Array size ? ";

cin >> n;

int size= n;

int array[size];

for (count=1; count<=n; count++){

cout << "Enter Number: ";

cin >> array[n];

sum = sum + array[n];

}

cout << "The sum is " << sum << endl;

avg = sum / n;

cout << "The average is " << avg << endl;

getch();

return 0;

}

int reverse(int arr[], int count)

{

Page 2: Computer Assignment

int temp;

for (int i = 0; i < count/2; ++i)

{

arr[i] = temp;

temp = arr[count-i-1];

arr[count-i-1] = arr[i];

arr[i] = temp;

cout << temp << " ";

return 0;

}

}

int main()

{

int choice;

cout<<"enter choice : ";

cin>>choice;

switch(choice)

{

case 1:cout<<"Maximum Number ";

case 2:cout<<"Minimum Number ";

case 3:

int sum;

cout<<"Sum and Average ";

sum =sum_avg();

cout<<sum ;

break;

case 4:int i,n,array[100];

cout<<"Array size : ";

cin>>n;

Page 3: Computer Assignment

for (i=1;i<=n;i++)

{

cout << "Enter Number: ";

cin >> array[n];

}

break;

case 5:cout<<"Press any key to exit...";

getch();

exit(0);

}

getch();

return 0;

}

___________________________________________________________________________Question 2//2d array

#include<iostream.h>

#include<conio.h>

#include<process.h>

int main()

{

int choice;

int a[10][10];

int b[10][10];

int x,y,i,j;

cout<<"\nEnter the number of rows and columns :::\n\n";

cin>>x>>y;

Page 4: Computer Assignment

cout<<"\n\nEnter elements for Matrix A :::\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>a[i][j];

}

cout<<"\n";

}

cout<<"\n\nEnter elements for Matrix B :::\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cin>>b[i][j];

}

cout<<"\n";

}

cout<<"\n\nMatrix A :\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<"\t"<<a[i][j];

}

cout<<"\n\n";

}

Page 5: Computer Assignment

cout<<"\n\nMatrix B :\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<"\t"<<b[i][j];

}

cout<<"\n\n";

}

cout<<"Enter Choice : \n 1.Addition\n2.Subtraction\n3.Transpose\n4.Exit\n";

cin>>choice;

switch(choice)

{

case 1:cout<<"\n\nAddition of Matrix A and Matrix B :\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

cout<<"\t"<<a[i][j]+b[i][j];

}

cout<<"\n\n";

}

break;

case 2:cout<<"\n\nSubtraction of Matrix A and Matrix B :\n\n";

for(i=0;i<x;i++)

{

for(j=0;j<y;j++)

{

Page 6: Computer Assignment

cout<<"\t"<<a[i][j]-b[i][j];

}

cout<<"\n\n";

}

break;

case 3:cout<<"\n\nTranspose of Matrix A :\n\n";

for(i=0;i<y;i++)

{

for(j=0;j<x;j++)

{

cout<<"\t"<<b[i][j];

}

cout<<"\n\n";

}

break;

case 4:cout<<"Press any key...";

break;

exit(0);

}

getch();

return 0;

}

__________________________________________________________________________________

Question 3//2d strings

#include<iostream.h>

#include<conio.h>

#include<stdio.h>

Page 7: Computer Assignment

int main( )

{

int choice,I;

char STR1[80],STR2[80];

cout<<"Enter first string:";

gets(STR1);

cout<<"Enter second string:";

gets(STR2);

cout<<"Enter choice :\n1.Comparison of String\n2.Contantation of String\n3.Exit\n";

cin>>choice;

switch(choice)

{

case 1:cout<<"Comparison of 2 strings\n\n\n\n";

for (I=0; STR1[I]==STR2[I] && STR1[I]!= '\0' && STR2[I] != '\0'; I++);

{

if(STR1[I]-STR2[I]==0)

cout<<"Strings are equal";

else

cout<<"Strings are not equal";

}

break;

case 2:int I,l;

char str1[80],str2[80];

cout<<"Concantenation of 2 strings\n\n\n\n";

for(int l=0;str1[l]!='\0';l++);

{

for(int i=0;str2[i]!='\0';i++)

Page 8: Computer Assignment

str1[l++]=str2[i];

str1[l]='\0';

}

cout<<"\nThe first string after adding second string content is\n\n"<<str1;

case 3:cout<<"Press any key...";

break;

exit(0);

}

getch();

return 0;

}

__________________________________________________________________________________

Question 4#include <iostream>

#include <stdio.h>

#include <process.h>

#include <stdlib.h>

voidenterarray(int a[100],int m){cout<<"Enter the Size of 1-D Array ";

cin>>m;

for(inti=0;i<m;i++){

cout<<"Enter no ";

cin>>a[i];

}}

Page 9: Computer Assignment

intseano(int a[],int s)

{

s=0;intm,f=0;

for(inti=0;i<m;i++)

{

if(a[i]==s)

{

cout<<"Number Found "<<endl;

cout<<"Position = "<<i+1;

f=1;

break;

}

if(f==1)

cout<<"Number Not Found";

}

return(0);

}

voidenterstring(char st[],int n)

{

cout<<"Enter String 1-D String ";

gets(st);

}

intseach(char st[],int f)

{

for(inti=0;st[i]!='\0';i++)

{

Page 10: Computer Assignment

if(st[i]==f)

return(f);

}return(-1);

}

int main()

{

charst[100],f;

intch,m,n,i,a[100],s,b,r;

cout<<"1. Searching a Number. \n";

cout<<"2. Searching a Character. \n";

cout<<"3. Exit. \n";

cout<<"Enter your Choice";

cin>>ch;

switch(ch)

{

case 1:

enterarray(a,m);

cout<<"Enter Number to be Searched ";cin>>s;

b=seano(a,s);

break;

case 2:

enterstring(st,n);

cout<<"Enter Character to be Searched ";cin>>f;

r=seach(st,f);

Page 11: Computer Assignment

if(r==-1)

cout<<"Number not Found in the Array (-1).";

else

cout<<"Number is Present at "<<r+1<<" Position.";

break;

case 3:

cout<<"Exiting from the Program.";

}

return 0;

exit(0);

}

Question 5#include <iostream.h>

#include <stdio.h>

#include <process.h>

#include <stdlib.h>

void enter(int a[][10],intm,int n)

{

for(inti=0;i<m;i++)

{

for(int j=0;j<n;j++)

{

cout<<"Enter No. ";

cin>>a[i][j];

}}

Page 12: Computer Assignment

cout<<"Your Array. \n";

for(inti=0;i<m;i++)

{

for(int j=0;j<n;j++)

{

cout<<a[i][j]<<" ";

}cout<<"\n";

}

}

intsrows(int a[10][10],intsumr,intm,int n)

{

sumr=0;

for(inti=0;i<m;i++)

{

for(int j=0;j<n;j++)

sumr+=a[i][j];

}return(sumr);

}

intscols(int a[10][10],intsumc,intm,int n)

{

sumc=0;

for(int j=0;j<n;j++)

{

for(inti=0;i<m;i++)

Page 13: Computer Assignment

sumc+=a[i][j];

}return(sumc);

}

intsdiag(int a[10][10],intsumd,intm,int n)

{

sumd=0;

for(inti=0;i<m;i++)

{

for(int j=0;j<n;j++)

{

if(i+j==m-1 || i==j)

sumd+=a[i][j];

}

}return(sumd);

}

int main()

{

int a[10][10],m,n,x,y,z,ch,sumr,sumc,sumd;

cout<<"Enter an 2-D Array.\n";

cout<<"Enter the Size of Rows. ";cin>>m;

cout<<"Enter the Size of Columns. ";cin>>n;

cout<<"1. Sum of Rows. \n";

cout<<"2. Sum of Columns.\n";

cout<<"3. Sum of Diagonals. \n";

cout<<"4. Exit. \n";

Page 14: Computer Assignment

cout<<"Enter your Choice ";

cin>>ch;

enter(a,m,n);

switch(ch)

{

case 1:x=srows(a,sumr,m,n);

cout<<"Sum of Rows "<<x;break;

case 2:y=scols(a,sumc,m,n);

cout<<"Sum of Columns "<<y;break;

case 3:z=sdiag(a,sumd,m,n);

cout<<"Sum of Diagonals "<<z;break;

case 4:

cout<<"Exiting from Program";exit (0);}

return 0;

}

____________________________________________________________________________________________________________________________________________________________________

Page 15: Computer Assignment

COMPUTER ASSIGNMENT

C++

CODE + OUTPUT

UTKARSH BAJPAIXI ‘A’

R.N: 39