Important Program of C language in O level exam january 2019
Important Program of C language in O level exam january 2019
Write a program to calculate number of vowels (a, e, i, o, u) separately in the entered string.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch[200];
int a,e,i,o,u,index;
a=i=o=u=e=0;
clrscr();
printf("Enter your
string>> ");
gets(ch);
for(index=0;ch[index]!='\0';index++)
{
switch(ch[index])
{
case 'a' :
a++;
break;
case 'A' :
a++;
break;
case 'e':
e++;
break;
case 'E':
e++;
break;
case 'o':
o++;
break;
case 'O':
o++;
break;
case 'i':
i++;
break;
case 'I':
i++;
break;
case 'u':
u++;
break;
case 'U':
u++;
break;
}
}
printf("\nVowel
frequency\nA=%d\nE=%d\nI=%d\nO=%d\nU=%d",a,e,i,o,u);
getch();
}
Write a function to read a two dimensional matrix along with the number of rows and columns in it, from the user. The contents of the array and the number of rows and columns should be passed back to the calling function.
#include<stdio.h>
#include<conio.h>
#define ROW 10
#define COL 10
void print(int (*)[],int,int);
void main()
{
int ar[ROW][COL],r,c,i;
clrscr();
for(r=0,i=1;r<ROW;r++)
{
for(c=0;c<COL;c++)
{
ar[r][c]=i;
i++;
}
}
printf("Enter Number
of row and column to read>> ");
scanf("%d%d",&r,&c);
print(ar,r,c);
}
void print(int (*ar)[COL],int r,int c)
{
int a,b;
if(r>ROW ||
c>COL)
printf("\nOut of range row or column");
else
{
for(a=0;a<r;a++)
{
for(b=0;b<c;b++)
printf("%d\t",ar[a][b]);
printf("\n");
}
getch();
}
}
How does passing an array as an argument to a function differ from call by value?
An array is a collection of similar data type which
occupies continuous location on memory. Name of array is constant pointer which
point (store address of first element of array) first element. When we passed
an array to a function as an argument then we pass address of first element and
if any change made inside of function to passing array then its effect original
location because it is call by reference.
So when an array is passed to a function as an
argument then it’s always being pass by reference.
As in figure array is passed to
function as argument when array name ar is passed then address of first element
which is 10 is passed. So any change made by function will affect original
data. Following code is example to demonstrate this fact. All element of
passing array will be increase by 10.
#include<stdio.h>
#include<conio.h>
int updatear(int *);
void main()
{
int ar[5],i;
clrscr();
for(i=0;i<5;i++)
{
printf("Enter Number>> ");
scanf("%d",&ar[i]);
}
printf("\nArray data
before function call\n");
for(i=0;i<5;i++)
printf("\t%d",ar[i]);
updatear(ar);
printf("\nArray data
after after function call \n");
for(i=0;i<5;i++)
printf("\t%d",ar[i]);
getch();
}
int updatear(int *ar)
{
int i;
for(i=0;i<5;i++)
ar[i]=ar[i]+10;
}
Comments
Post a Comment