ARRAYS ONE DIMENSION 1 D IN C LANGAUGE

  • Slides: 8
Download presentation
ARRAYS : ONE DIMENSION (1 -D) IN C++ LANGAUGE

ARRAYS : ONE DIMENSION (1 -D) IN C++ LANGAUGE

Q / write a program for read array 1 -D a[10], then calculate the

Q / write a program for read array 1 -D a[10], then calculate the number of positive and negative elements? #include<iostream. h> void main() { int a[10]; int pos=0, neg=0; for (int i=0; i<10; i++) cin>>a[i]; for (i=0; i<10; i++) if (a[i]>=0) pos++; else neg++; cout<<" Count of POS. number = "<<pos; cout<<" Count of NAG. number = "<<neg; }

Q//write a program to find the maximum and minimum value from an array a[10]?

Q//write a program to find the maximum and minimum value from an array a[10]? #include<iostream. h> int main() { int a[10]; int i; int larg=0, small=0; cout<<"enter 5 integer number : " <<endl; for (i=0; i<10; i++) cin>>a[i]; larg=a[0]; small=a[0]; for (i=0; i<10; i++) {if (a[i]>larg) larg=a[i]; if (a[i]<small) small=a[i]; } cout<<"largest value = "<<larg<<endl; cout<<"smallest value = "<<small<<endl; }

Q / write a program to switch the first half with the second half

Q / write a program to switch the first half with the second half in a array 1 -D, A[10]? #include<iostream. h> void main() { int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int i, j, t; for(i=0; i<5; i++) {t=a[i]; a[i]=a[5+i]; a[5+i]=t; } for(i=0; i<10; i++) cout<<" "<<a[i]; } 2 1 2 3 4 1 5 6 3 t 7 8 9 10

Note/ to find the inverse of a matrix A[10]? for (i=0; i<5; i++) {t=a[i];

Note/ to find the inverse of a matrix A[10]? for (i=0; i<5; i++) {t=a[i]; a[i]=a[10 -i-1]; a[10 -i-1]=t; } 2 1 2 3 4 1 5 6 7 8 3 t 9 10

Q/Write a program to shift array a[10] one site to left side? #include<iostream. h>

Q/Write a program to shift array a[10] one site to left side? #include<iostream. h> void main() { 2 int a[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; int i, t; t=a[0]; 1 2 3 4 5 6 7 8 9 for(i=1; i<10; i++) 1 a[i-1]=a[i]; 3 a[9]=t; t for(i=0; i<10; i++) cout<<" "<<a[i]; } 10

Homework Q 1/Write a program to shift array a[10] one site to right side?

Homework Q 1/Write a program to shift array a[10] one site to right side? Q 2/ Write a program to find the second highest value in a matrix a[10]? Q 3/Write a program to replace the contents of even location with odd location in the matrix of a [10]? Q 4//W. P. to apply ascending order on array 1_D, A[10]?