Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Arrays

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];

if(x[i]>0)
{
p=p+1;
}
if(x[i]<0)
{
n=n+1;
}
if(x[i]==0)
{
z=z+1;
}
}

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.

#include<iostream.h>
#include<conio.h>
void main(void)
{
int x[4][5],r,c;
clrscr();
for(r=0;r<4;r++)
{
for(c=0;c<5;c++)
{

Cout<<"\n enter the values ";
Cin>>x[r][c];
}

}
for(r=0;r<4;r++)
{
for(c=0;c<5;c++)
{

Cout<<x[r][c];


}
Cout<<"\n";
}
getch();

}

5. Write a program for 3x3 two dimensional matrix addition.
#include<iostream.h>
#include<conio.h>
void main(void)
{
int sum,x[2][3],r,c;
clrscr();
for(r=0;r<2;r++)
{
for(c=0;c<3;c++)
{
Cout<<"enter the values ";
Cin>>x[r][c];
}
}
for(r=0;r<2;r++)
{
sum=0;
for(c=0;c<3;c++)
{
sum=sum+x[r][c];
cout<<\t"<<x[r][c];

}
Cout<<"\n the sum of values of <<r<<rows = << \t"<<sum;
Cout<<"\n";
}
getch();
}

6. Illustrate with an example how to pass one dimensional array to a function.

#include<iostream.h>
#include<conio.h>
void show(int x[],int size)
Void main(void)
{
clrscr();
int i;
int []a={5,10,15,20,25,30};
Show(a,6);

}
getch();

}
void show(int x[],int size)
{
for(int i=0;i<size;i++)
{
cout<<\n<<x[i];

}

Pointers

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--------------------------------------------

You might also like