The document discusses arrays and pointers in C programming. It provides 6 examples of using arrays, including reading and displaying values, counting positive/negative/zero values, and sorting arrays. It also provides 3 examples of using pointers, such as introducing pointers, adding two numbers with pointers, and swapping values using a pointer to a function. The document covers basic concepts and applications of arrays and pointers.
The document discusses arrays and pointers in C programming. It provides 6 examples of using arrays, including reading and displaying values, counting positive/negative/zero values, and sorting arrays. It also provides 3 examples of using pointers, such as introducing pointers, adding two numbers with pointers, and swapping values using a pointer to a function. The document covers basic concepts and applications of arrays and pointers.
The document discusses arrays and pointers in C programming. It provides 6 examples of using arrays, including reading and displaying values, counting positive/negative/zero values, and sorting arrays. It also provides 3 examples of using pointers, such as introducing pointers, adding two numbers with pointers, and swapping values using a pointer to a function. The document covers basic concepts and applications of arrays and pointers.
1. Read and display 5 integer values using an array.
#include<iostream.h> #include<conio.h> void main(void) { int x[5],i; clrscr(); for(i=0;i<10;i++) { cout<<"enter the<<i+1<< number "; cin>>x[i]; } for (i=0;i<10;i++) { cout<<\n number <<i+1<<x[i];
} getch(); }
2. Write a program to count total number of positive values, negative values and zeros in one dimensional array. #include<iostream.h> #include<conio.h> void main(void) { int x[20],i,n=0,p=0,num,z=0; clrscr(); cout<<"enter the size of an array: "; cot<<num; for(i=0;i<num;i++) { cout<< no<<i+1; cin>>x[i];
cout<<"total no of positive values : <<p; cout<<"\ntotal no of negative values: <<n; cout<<\ntotal no of zeroes values: "<<z; getch(); }
3. Write a program to sort all the values in an integer array in descending or ascending order.
#include<iostream.h> #include<conio.h> void main(void) { int x[5],j,i,temp; clrscr(); for(i=0;i<5;i++) { cout<<"enter the number: "; cin>>x[i]; } for(i=0;i<4;i++) { for(j=i+1;j<5;j++) {
if(x[i]>x[j]) {
temp=x[i]; x[i]=x[j]; x[j]=temp; }
}
} for(i=0;i<5;i++) { cout<<x[i];
}
getch();
}
4. Write a program to print mark sheet of a student taking marks for 6 subject in an array. #include<stdio.h> #include<conio.h> void main(void) { int marks[6],i,sum=0,avg=0; clrscr(); for(i=0;i<6;i++) { cout<<"\n enter the marks for<<i+1<< subjects: "; cin>>marks[i]; sum=sum+marks[i]; } for(i=0;i<6;i++) { cout<<"\n marksheet : "; cout<<marks[i]<<\n;
} Cout<<"total marks obtain: "<<sum); avg=sum/6; cout<<"\n average: "<<avg; getch();
} 5. Read and display integer values in a matrix having 4 rows and 5 columns using two dimensional array.
1. Introduction to pointer. #include<iostream.h> #include<conio.h> void main(void) { clrscr(); int x,*ptr; x=100; ptr=&x;
/*value of x*/ cout<<"\n value of x = <<x; /*address of x*/ cout<<"\n address of x ="<<&x; cout<<"\n address of x = "<<ptr; /*value of x*/ cout<<"\n value of x = "<<*(&x); /*value of pointer*/ cout<<"\nvalue of ptr= "<<ptr; /*addresss of pointer*/ cout<<"\n address of ptr= "<<&ptr; /*value of x*/ cout<<"\n value of x= "<<*(ptr); getch();
}
2. Addition of two numbers using pointers #include<iostream.h> #include<conio.h> void main(void) { clrscr(); int x,y; int *ptr1,*ptr2; cout<<enter the first number; cin>>x; cout<<enter the second number; cin>>y; ptr1=&x; ptr2=&y; int z=(*ptr1)+(*ptr2); cout<<addition= <z; getch(); }
3. Write a program to swap two integer values using pointer to function.
/*(pointer to a function)call by refrence*/ #include<iostream.h> #include<conio.h> void swap(int*,int*); void main (void) { clrscr(); int x, y; cout<<"enter the value of x "); cin>>x; cout<<"enter the value of y "); cin>>y; swap(&x,&y); cout<<" the value for x = \n"<<x; cout<<" the value for y = \n"<<y;
getch();
} void swap(int*a,int*b) {
int temp; temp=*a; *a=*b; *b=temp; cout<<"after swapping the value of x= \n",*a); cout<<"after swapping the value of y= \n",*b);
} -------------------------------------------------the end of arrays and pointers--------------------------------------------