C programming:- Storage classes and Pointers - Part 4
/*Declare three pointer variables
to store a character, a character string and an integer respectively. Input
values into these variables. Display the address and the contents of each
variable.( (MG University NOV 2009)
*/
#include<stdio.h>
main()
{
int a,*pa;
char c1,*pc;
char ch[30],*pch;
printf("enter an
integer,character and a string");
scanf("%d%1s%s",&a,&c1,ch);
printf("\nwithout using pointers
integer=%d character=%c string=%s",a,c1,ch);
pa=&a;
pc=&c1;
pch=ch;
printf("\nwith using pointers
integer=%d character=%c string=%s",*pa,*pc,pch);
}
/*OUTPUT
enter
an integer,character and a string
1
a hello
without
using pointers integer=1 character=a string=hello
with
using pointers integer=1 character=a string=hello
*/
/*WAP using pointers to multiply
two integers. (MG University NOV 2009)*/
#include<stdio.h>
main()
{
int a,b,*pa,*pb;
printf("enter two integers to
multiply:");
scanf("%d%d",&a,&b);
printf("\nwithout using pointers
res=%d",a*b);
pa=&a;
pb=&b;
printf("\nwith using pointers
res=%d",(*pa)*(*pb));
}
/*
OUTPUT
enter
two integers to multiply:3 4
without
using pointers res=12
with
using pointers res=12
*/
Swap two numbers using
pointers
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *a;
*a = *b;
*b = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Swapping numbers using call by reference
#include <stdio.h>
int main()
{
int x, y, *a, *b, temp;
printf("Enter the value of x and y\n");
scanf("%d%d", &x, &y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
a = &x;
b = &y;
temp = *a;
*a = *b;
*b = temp;
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
Swapping numbers using call by reference
#include
<stdio.h>
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
void swap(int*, int*);
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);
printf("Before Swapping\nx = %d\ny = %d\n", x, y);
swap(&x, &y);
printf("After Swapping\nx = %d\ny = %d\n", x, y);
return 0;
}
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
USING POINTERS WRITE A PROGRAM TO
FIND THE BIGGEST OF N NUMBERS IN AN ARRAY ?
#include<stdio.h>
#include<conio.h>
void main()
{ int a[10],i,n,*large,*small;
clrscr();
printf("\nEnter number of elements of the array:");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
large=a;
small=a;
for(i=0;i<n;i++)
{ if(*large<a[i])
{ large=&a[i];
}
if(*small>a[i])
{ small=&a[i];
}
}
printf("\nThe largest number is %d",*large);
printf("\nThe smallest number is %d",*small);
getch();
}
WRITE
A C PROGRAM TO COUNT THE NUMBER OF VOWELS IN AN ARRAY OF CHARACTERS USING
POINTERS ?
#include<conio.h>
#include<stdio.h>
#include<string.h>
void main()
{
char *s,a[50];
int i=0;
clrscr();
printf("ENTER THE STRING ");
gets(a);
s=a;
while(*s!='\0')
{
switch(*s)
{
case 'a':i++;
break;
case 'e':i++;
break;
case 'i':i++;
break;
case 'o':i++;
break;
case 'u':i++;
break;
case 'A':i++;
break;
case 'E':i++;
break;
case 'I':i++;
break;
case 'O':i++;
break;
case 'U':i++;
break;
}
s++;
}
printf("THE NO. OF VOWELS = %d
",i);
getch();
}
..
Using pointers ,write a C program to add
two square matrices
Without
pointers:
#include<stdio.h>
main()
{
int
a[10][10],b[10][10],c[10][10];
int
i,j,m,n;
printf("enter
the order of matrices:");
scanf("%d%d",&m,&n);
printf("enter
mat A elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter
mat B elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("\nresultant
mat C:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d
",c[i][j]);
}
printf("\n");
}
}
With
pointers:
#include<stdio.h>
main()
{
int
a[10][10],b[10][10],c[10][10];
int
i,j,m,n;
printf("enter
the order of matrices:");
scanf("%d%d",&m,&n);
printf("enter
mat A elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",(*(a+i)+j));
}
}
printf("enter
mat B elements");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",(*(b+i)+j));
}
}
printf("\nresultant
mat C:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
*(*(c+i)+j)=
*(*(a+i)+j)+ *(*(b+i)+j);
printf("%d
",*(*(c+i)+j));
}
printf("\n");
}
}
Using
functions:
#include<stdio.h>
void readmat(int pa[10][10],int
p,int q); //pass matrix and its order
void sum(int pa[10][10],int
pb[10][10],int p,int q); /*pass
two matrices and its order*/
void disp(int pa[10][10],int p,int
q); //pass matrix and
order
int a[10][10],b[10][10],c[10][10];
int i,j;
main()
{
int
m,n;
printf("enter
the order of matrices:");
scanf("%d%d",&m,&n);
printf("enter
mat A elements");
readmat(a,m,n);
printf("enter
mat B elements");
readmat(b,m,n);
sum(a,b,m,n);
printf("mat
A:\n");
disp(a,m,n);
printf("mat
B:\n");
disp(b,m,n);
printf("\nresultant
mat C:\n");
disp(c,m,n);
}
void readmat(int pa[10][10],int
p,int q)
{
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",(*(pa+i)+j));
}
}
}
void sum(int pa[10][10],int
pb[10][10],int p,int q)
{
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
*(*(c+i)+j)=
*(*(a+i)+j)+ *(*(b+i)+j);
}
}
}
void disp(int pa[10][10],int p,int
q)
{
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d
",*(*(pa+i)+j));
}
printf("\n");
}
}
/*OUTPUT
enter the order of matrices:2 2
enter mat A elements 1 2 3 4
enter mat B elements1 1 1 1
mat A:
1 2
3 4
mat B:
1 1
1 1
resultant mat C:
2 3
4 5
*/
WAP using pointers to sort the given
list of numbers in descending order. .(MG University NOV 2010)
Without pointers:
#include <stdio.h>
main()
{
int arr[30],i,j,tmp,n;
printf("Enter limit of numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&arr[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if( arr[j] < arr[j+1])
{
tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
printf("\n\nAfter Sorting in descending order\n");
for(i=0;i<n;i++)
printf("%d\n",arr[i]);
}
/*OUTPUT
Enter limit of numbers : 5
13 2 1 5 34
After Sorting in descending order
34
13
5
2
1
*/
With pointers:
#include <stdio.h>
main()
{
int arr[30],i,j,tmp,n;
printf("Enter limit of numbers : ");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",arr+i);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if( *(arr+j) < *(arr+j+1))
{
tmp = *(arr+j);
*(arr+j) = *(arr+j+1);
*(arr+j+1) = tmp;
}
}
}
printf("\n\nAfter Sorting in descending order\n");
for(i=0;i<n;i++)
printf("%d\n",*(arr+i));
}
/*OUTPUT
Enter limit of numbers : 5
13 2 1 5 34
After Sorting in descending order
34
13
5
2
1
*/
Write a C function that receives a
number n and returns a pointer to a character string containing the name of the
corresponding day. The day names should be stored in a static table of
character strings local to the function. (MG University MAY 2009)
#include<stdio.h>
char* fun(int a);
main()
{
int
n;
char
*ch;
printf("enter
a number(1-7)");
scanf("%d",&n);
ch=fun(n);
printf("output-->%s",ch);
}
char* fun(int a)
{
char c[10][10]={"monday","tuesday","wednesday","thursday","friday","saturday","sunday"};
switch(a)
{
case
1: return(c[0]);
break;
case
2: return(c[1]);break;
case
3: return(c[2]);break;
case
4: return(c[3]);break;
case
5: return(c[4]);break;
case
6: return(c[5]);break;
case
7: return(c[6]);break;
default
:printf("invalid"); break;
}
}
/*
enter a number(1-7)4
output-->thursdaystudent@ubuntu:~$
./a.out
enter a number(1-7)1
output-->mondaystudent@ubuntu:~$
./a.out
enter a number(1-7)9
invalidoutput-->(null)student@ubuntu:~$
./a.out
enter a number(1-7)6
*/
WAP to find the largest word of a given
sentence, using pointers.
Pgm Without pointers (try it with
pointers)
#include<stdio.h>
#include<string.h>
main()
{
char
sen[30];
char
s[30][30],s1[30];
int
c,i,j,k,m,l1,l2;
printf("enter
a sentence:");
gets(sen);
c=strlen(sen);
j=0;k=0;
for(i=0;i<c;i++)
{
while(sen[i]!='
')
{
s[j][k]=sen[i];
i++;k++;
}
if(sen[i]=='
')
{ s[j][k]='\0';j++; k=0;}
}
m=j;
printf("
2d array: ");
for(i=0;i<m;i++)
printf("%s\n",s[i]);
for(i=0;i<m;i++)
{
printf("
%s len= %d\n",s[i],strlen(s[i]));
}
l1=strlen(s[0]);
for(i=0;i<m;i++)
{
if(l1<strlen(s[i]))
{l1=strlen(s[i]);
strcpy(s1,s[i]);}
}
printf("longest
word= %s len= %d\n",s1,l1);
}
/*
student@ubuntu:~$ ./a.out
enter a sentence:hai students study well
2d array: hai
students
study
well
hai len= 3
students len= 8
study len= 5
well len= 4
longest word= students len= 8*/
delete
duplicate words in a string
#include <stdio.h>
#include <string.h>
main()
{
char a[100], b[20][20];
int i, j = 0, k = 0, n, m;
printf("enter the string\n");
scanf("%[^\n]", a);
for (i = 0;a[i] != '\0';i++)
{
if (a[i] == ' ')
{
b[k][j] = '\0';
k++;
j = 0;
}
else
{
b[k][j] = a[i];
j++;
}
}
b[k][j] = '\0';
for (i = 0;i <= k;i++)
{
for (j = i + 1;j <= k;j++)
{
if (strcmp(b[i], b[j]) == 0)
{
for (m = j;m <= k;m++)
strcpy(b[m], b[m + 1]);
k--;
}
}
}
for (n = 0;n <= k;n++)
{
printf("%s\n", b[n]);
}
}
/* OUTPUT
enter the string
hai hai students. you should study study
well.
hai
students.
you
should
study
well.
*/
Comments
Post a Comment