Showing posts with label Array. Show all posts
Showing posts with label Array. Show all posts

Sunday, June 6, 2021

Arranging values in an array in zig zag manner

 Few values are to be taken from user and the values will be arranged in ascending order but in a zig zag manner. The smallest value will be placed at the extreme left side (at zero index location) and the next value will be at the extreme right location (last index location). Similarly the third value in the ‘1’ index location and 4rth value at the (size-2) location of the array and so on.


#include < stdio.h>
void main()
{
 int c=0,min,minl,k,arr[100],i,j,n,t,x=0,y;
 clrscr();
 printf("\nHow many elements:");
 scanf("%d",&n);
 for(i=0;i< n;i++)
 {
  printf("\nValue=");
  scanf("%d",&arr[i]);
  }
  printf("\nOriginal=");
      for(i=0;i< n;i++)
    printf("%d   ",arr[i]);
  y=n-1;
 for(i=x;i<=y;i++)
 {
  min=arr[i];
  minl=i;
    for(j=i+1;j<=y;j++)
  {
   if(arr[j]< min)
   {
    min=arr[j];
    minl=j;
    }
    }
    if(c%2==0)
    {
     t=arr[x];
     arr[x]=min;
     arr[minl]=t;
     x++;
     }
     else
    {
    t=arr[y];
     arr[y]=min;
     arr[minl]=t;
     y--;
     i--;
     }
     c++;
     }
     printf("\nFinal=");
    for(i=0;i< n;i++)
    printf("%d   ",arr[i]);
    getch();
    }

C Array And Recursive Function To Check Prime Number And Largest Number

 Details of the program: Write a C program that takes a 1-dimensional array arr as input (of size 10) and check if each element in arr is prime or not using recursion, and store the prime numbers in another array arr2

Subtract 2 from each element in arr2
Use recursion to find the largest element in arr2

   
   
   #include<stdio.h>
   int x;
   void main()
   {
    int arr1[10],arr2[10];
    int i;
    for(i=0;i<10;i++)
    {
    printf("\nElement No %d: ",(i+1));
    scanf("%d",&arr1[i]);
    }
    for(i=0;i<10;i++)
{

if(isPrime(arr1[i],arr1[i]/2))

arr2[x++]=arr1[i];
}
printf("\nPrime numbers are\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
for(i=0;i<x;i++)
arr2[i]=arr2[i]-2;

printf("\nAfter Deduction\n");
for(i=0;i<x;i++)
printf("%d ",arr2[i]);
i=getMax(arr2);
printf("\nMax Value=%d",i);
getch();
   }
  int isPrime(int n, int i)
{
    if(i == 1)
        return 1;   
    else
    {
        if(n%i == 0)
            return 0;
        else
            isPrime(n, i-1);    
    }
}
  int getMax(int a[])
{
    static int i = 0, max =- 9999;  
    if(i < x)   
    {
        if(max < a[i])
        max = a[i];
        i++;    
        getMax(a);   
    }
    return max;
}

C Array And Sorting

 Details of the program:  Write a C program that Takes a 1-dimensional array a as input (of size 20)
Now, sort the first 5 elements in descending order and sort the last 15 elements in ascending order (do not use any pre-defined function for sorting) in a using at most 2 loops. Replace the smallest element with -1 without using any more loop and display the final array elements

#include<stdio.h>
void main()
{

int arr[20];
int i,j,t,min,index;
for(i=0;i<20;i++)
{
printf("\nValues:");
scanf("%d",&arr[i]);
 
}
for(i=0;i<5-1;i++)
{
for(j=i+1;j<5;j++)
{
if(arr[i]<arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
for(i=5;i<20-1;i++)
{
for(j=i+1;j<20;j++)
{
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
}
}
if(arr[4]<arr[5])
arr[4]=-1;
else
arr[5]=-1;
printf("\nElements as follows\n");
for(i=0;i<20;i++)
printf(" %d",arr[i]);
getch();
}

Friday, November 5, 2010

Passing array to function in C Language

In the next example we will store some values in an array and the address of each location of the array will be passed to a function one after another . The function will display the value stored in the location.

#include< stdio.h>
 void dis(int *);
void main()
{
 int i;
 int arr[]={3,4,5,6};
 for(i=0;i< 4;i++)
 dis(&arr[i]);
 }
 void dis(int *p)
 {
 printf("\nValue=%d",*p);
}

Now the same program is modified so that  the entire array is passed to the function at a time.

#include<stdio.h>
 void dis(int *,int);
void main()
{
  int arr[]={3,4,5,6};
  dis(&arr[0],4);
 }
 void dis(int *p,int no)
 {
 int i;
 for(i=0;i< no;i++)
 {
 printf("\nValue=%d",*p);
 p++;
 }
}


Friday, August 27, 2010

Array and pointers in C programming language


When an array is declared in C language, compiler allocates a base address and sufficient amount of storage to hold the elements. The base address is the location of the first element (index 0) .The compiler also defines the array name as a constant pointer to the first element. Suppose we declare an array as follows:
                                   int arr [4] = [1,2,3,4};

The array name arr is defined as a pointer pointing to the first element arr [0] and therefore the value of arr is 1000(say),the location where arr [0] is stored. arr = &arr [0] = 1000

The address of the second element can be written as either &arr [1] or arr+1.Here the expression arr+1 represents an address rather than an arithmetic expression. Now if we declare ptr as a pointer variable and want to point the array arr, then the statement looks like: ptr = arr;This is equivalent to ptr = &arr [0];
We can access every value of arr using ptr++ to move from one element to another.*(ptr+2) gives the value of arr [2]. The pointer accessing method is much faster than array indexing.

Here is a C Program to demonstrate the above feature


#include< stdio.h>
void main ()
{
int *ptr, sum, i;
int arr [4]={1,2,3,4};
clrscr ();
i=0;
sum=0;
ptr=arr;
printf ("Element   Value     Address\n");
while (i< 4)
{
 printf ("arr [%d]    %d         %u\n", i, *ptr, ptr);
 sum=sum+*ptr++;
/*note ‘postfix’ operator.*/
 i++;
  }
 printf ("\nSum is:-%d", sum);
 getch ();
 }

Monday, August 23, 2010

Pointers in C Language

What is pointer


Pointer is the flavor of C Language. It’s a special type of variable that can hold the address of same type of variable. So we can say that variables deals with value while pointers deals with address in C Language.

Consider the following declaration:-
int i =50;
This declaration tells the C compiler to-
Reserve space in memory to store an integer value
Associate the name i  with the memory location
store the value 5 at this location

This feature can be demonstrate with the help of a chart
 Location Name     i
 Value                    50
Address                1000

In C Language, &is called address operator while * is called value at address  or indirection operator.

C program to illustrate the use of pointer

#include< stdio.h >
 void main()
 {
  int i=5;
  printf("\nValue stored is=%d",i);
  printf("\nAddress of the location is=%u",&i);
  printf("\nValue stored is=%d",*(&i));
  }


From the above program it is clear that &i returns the address of i.So, this address can be stored in a variable and address of a variable can be stored in pointer.

The above C  program on pointeris modified as follows:-

#include< stdio.h >
 void main()
 {
  int i=5;
  int *j;
  j=&i;
  printf("\nvalue stored in the pointer(address of the variable'i')is=%u",j);
  printf("\nAddress of the location (through address operator)is=%u",&i);
  printf("\nValue stored (displayed through pointer)is=%d",*j);
  printf("\nValue stored (displayed through variable)is=%d",i);
  printf("\nAddress of the pointer is=%u",&j);
  }




Sunday, August 15, 2010

2 d array of character type program to demonstrate students grade


Here is another program on C Language character type array. Accept name of the students and exam grade of each student, determine an average grade for each student, and display name, the individual exam grade, the calculated average and the rank for each student.


   #include< stdio.h >
   #include< string.h >
   void main()
   {
   char name[4][30];
   char grade[20],c,dummy[30];
   int i,j,t=0;
   clrscr();
   printf("If the mark is >=80 put 'A',>= 65 but <80\n");
   printf("put 'B' grade.Marks <65, put 'C' grade\n");
    for(i=0;i< 4;i++)
    {
    puts("\nEnter the name:-");
    gets(name[i]);
    puts("\nEnter the GRADE:-");
    c=getchar();
    c=toupper(c);
    grade[i]=c;
    fflush(stdin);
    }
   grade[i]=NULL;
   for(i=0;i< 4;i++)
    {
     for(j=i+1;j <4;j++)
      {
       if(grade[i] >grade[j])
            {
             c=grade[i];
             grade[i]=grade[j];
             grade[j]=c;
             strcpy(dummy,name[i]);
             strcpy(name[i],name[j]);
             strcpy(name[j],dummy);
             }
            }
       }
   puts("\nPress any KEY to see the Result.......");
   getch();
  for(i=0;i< 4;i++)
  {
   printf("%s   %c\n",name[i],grade[i]);
  }
  for(i=0;I <4;i++)
  {
   if(grade[i]=='A')
    t=t+80;
    else if(grade[i]=='B')
    t=t+65;
    else
    t=t+64;
   }
   t=t/4;
   if(t>=80)
   printf("\nAverage grade is 'A'");
   else if(t>=65)
   printf("\nAverage grade is 'B'");
   else
   printf("\nAverage grade is 'C'");
 getch();
 }



Sunday, August 8, 2010

C programs on string to check palindrome


Since the last few posts of our C programming language tutorial, we are continuing on array – character type. Actually string in C Language programming is a vast chapter and needs more time. We’ll see more and more programs on string in this C language programming tutorial.

Another string function is void strcpy (char ch1[],char ch2[]).This function copies one string to another. The first argument is the destination and the second argument is the source. The value in the second array will copied on the first array. If the first array has any value then the value will be lost and it will hold the value of the second argument, while the value in the second array will remain unchanged.

#include< stdio.h >
#include< string.h >
void main()
{
char ch1[50],ch2[50];
int i;
clrscr();
puts("Enter the first string:-");
gets(ch1);
puts ("Enter the second string:-");
gets (ch2);
printf ("Before calling the function the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
strcpy (ch1, ch2);
printf ("\nAfter calling the function, the strings are:-\n");
printf ("%s", ch1);
printf ("\n%s", ch2);
 getch ();
 }
 }

Palindrome checking program on string
.
#include< stdio.h >
#include< string.h >
void main ()
{
char ch1 [50], ch2 [50];
int i, x=0;
clrscr ();
puts ("Enter the string:-");
gets (ch1);
for (i=strlen (ch1)-1; i >=0; i--)
{
ch2[x]=ch1 [i];
x++;
}
ch2[x] ='\0';
if (strcmp (ch1, ch2) ==0)
puts ("The string is palindrome.");
else
puts ("The string is not palindrome.");
getch ();
 }

Thursday, August 5, 2010

C programs on C array to display in reverse order and searching


In the next few posts we will work on C array – char type. We will see Different type of  programs on C array.


C program on array to search a specific character from it

Take a string and a special character from the user and display whether the character is available in the string. If available then how many times ?

#include< stdio.h >
#include< string.h >
void main()
{
 char ch[20],c;
 int i=0,counter=0;
 clrscr();
 puts("Enter the string:-");
 gets(ch);
 puts("Enter the character:-");
 scanf("%c",&c);
 for(i=0;i< strlen(ch);i++)
 {
 if(c==ch[i])
 counter++;
 }
 if(counter!=0)
 printf("The character '%c' occurs %d times in the string.",c,counter);
 else
 printf("The character '%c' is not available in the string.",c);
 getch();
 }

C program to display a string in reverse order

#include< stdio.h >
#include< conio.h >
void main()
{
 char str1[100],str2[100];
 int i=0,n=0,j;
 clrscr();
 printf("\nEnter the string::");
 do{
    str1[i]=getchar();
    i++;
    }while(str1[i-1]!='\n');
    str1[i-1]='\0';
 printf("\nThe string is \t%s",str1);
 printf("\nNow the string will be displayed in reversed order\n");
 for(j=i-2;j >=0;j--)
 {
  str2[n]=str1[j];
  n++;
 }
 str2[n]='\0';
 printf("\nReversed string\t%s",str2);
 getch();
}

Saturday, July 24, 2010

Programs on array using C Programming Langusge


Since the last few postings we are in C Language array.  Today’s topic in our C programming tutorialis also c array . We’ll solve some programs on array using C Language.


 Program on identity matrices

#include< stdio.h >
void main ()
{
int mat1[4][4],mat2[4][4],i,j,x=3;
clrscr();
printf("Program on identity matrix.\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  if(i==j)
   mat1[i][j]=1;
  else
   mat1[i][j]=0;
  }
}
printf("The identity matrix is :-\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  printf("%4d",mat1[i][j]);
 }
 printf("\n");
}
printf("\n AND\n");
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  if(j==x)
   mat1[i][j]=1;
  else
   mat1[i][j]=0;
  }
  x--;
}
for(i=0;i< 4;i++)
{
 for(j=0;j< 4;j++)
 {
  printf("%4d",mat1[i][j]);
 }
 printf("\n");
}
getch ();
}

In this C Language program, we’ll work on matrix. Given a 2-D matrix of order 3*3 .Search an element x if the element in the matrix, display that the search is successful and display the position at which the element occurs. If the search is not successful-display that element is not in the matrix.


 #include< stdio.h>
void main ()
 {
 int mat[3][3],i,j,x;
 clrscr();
 printf("\nNow we will enter the values(integer) for the matrix.");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("\nValue:-");
   scanf("%d",&mat[i][j]);
   }
 }
 printf("\nEnter the specific value to be searched:-");
 scanf("%d",&x);
 clrscr();
 printf("\nThe matrix:-\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("%4d",mat[i][j]);
   }
  printf("\n");
 }
for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   if(x==mat[i][j])
   break;
   }
  if(j!=3)
  break;
 }
 if(i!=3)
 {
 printf("Your input value exists in row no %d and column no %d",i+1,j+1);
 }
 else
 {
 printf("Value not available.");
 }
 getch();
 }

Same  array program with some modification

 #include< stdio.h>
 void main ()
 {
 int mat[3][3],i,j,n,x[3],y[3],a=0;
 clrscr();
 printf("\nNow we will enter the values(integer) for the matrix.");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("\nValue:-");
   scanf("%d",&mat[i][j]);
   }
 }
 printf("\nEnter the specific value to be searched:-");
 scanf("%d",&n);
 clrscr();
 printf("\nThe matrix:-\n");
 for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   printf("%4d",mat[i][j]);
   }
  printf("\n");
 }
for(i=0;i< 3;i++)
 {
  for(j=0;j< 3;j++)
  {
   if(n==mat[i][j])
   {
    x[a]=i+1;
    y[a]=j+1;
    a++;
    }
   }
 }
 if(a==0)
 printf("Value does not exists in the matrix.");
 else
 {
  printf("The value exists in the matrix and locations are:-\n");
  for(i=0;i< a;i++)
  {
  printf("%d and %d\n",x[i],y[i]);
  }
 }
  getch();
 }

Tuesday, June 15, 2010

C program to insert an element in the array

Today in our C  tutorial we will discuss about two operations on C array

To crerate an array and then to insert a value within the array.

#include < stdio.h>
void insert(int a[],int n)
{
int i,loc,temp,val;
printf("\nEnter the location where you want to insert(1 to %d):",n);
scanf("%d",&amp;loc);
printf("\nEnter the value:");
scanf("%d",&amp;val);
temp=a[loc-1];
a[loc-1]=val;
for(i=loc;i &lt;= n;i++)
{
loc=a[i]; a[i]=temp; temp=loc;
}
}

void main()
{
int arr[20],i,n;
clrscr();
printf("\nHow many values you want to insert(within 19):");
scanf("%d",&amp;n);
for(i=0;i &lt; n;i++)
{
printf("\nValue=");
scanf("%d",&amp;arr[i]);
}
insert(arr,n);
printf("\nAfter insertion\n");
for(i=0;i &lt; =n;i++)
{
printf("\nValue=%d",arr[i]);
}
getch();
}


C programs on array

Program to  crerate an C array and then to insert a value within the array.

#include < stdio.h >
void insert(int a[],int n)
{
int i,loc,temp,val;
printf("\nEnter the location where you want to insert(1 to %d):",n);
scanf("%d",&loc);
printf("\nEnter the value:");
scanf("%d",&val);
temp=a[loc-1];
a[loc-1]=val;
for(i=loc;i<=n;i++)

{
loc=a[i];
a[i]=temp;
temp=loc;
}
}
void main()
{
int arr[20],i,n;
clrscr();
printf("\nHow many values you want to insert(within 19):");
scanf("%d",&n);
for(i=0;i < n;i++)

{
printf("\nValue=");
scanf("%d",&arr[i]);
}
insert(arr,n);
printf("\nAfter insertion\n");
for(i=0;i < =n;i++)

{
printf("\nValue=%d",arr[i]);
}
getch();
}

C Program on deletion of element from array


#include < stdio.h >
void del(int a[],int n)
{
int i,loc;
printf("\nEnter the location which you want to delete:");
scanf("%d",&loc);
for(i=loc-1;i < n;i++)

{
a[i]=a[i+1];
}
}
void main()
{
int arr[20],i,n;
clrscr();
printf("\nHow many values you want to insert(within 19):");
scanf("%d",&n);
for(i=0;i < n;i++)

{
printf("\nValue=");
scanf("%d",&arr[i]);
}
del(arr,n);
printf("\nAfter deletion\n");
for(i=0;i < n-1;i++)

{
printf("\nValue=%d",arr[i]);
}
getch();
}

Two-Dimensional or 2 d Arrays

2 d array is actually array of arrays. To declare a two-dimensional array variable, specify each additional index using another set of square brackets. For example, to declare a two-dimensional integer array named arr, the syntax is:int arr [x][y];
This means that an array of size 'x' will be created, while within each index there will be another array of size 'y'.
Consider the following example:-
Marks obtained in three subjects by three students is

Roll no. Math Physics Chemistry

1 78 65 60
2 87 56 65
3 77 78 70

If we were asked to store the marks obtained by Roll no.1 ,then we can simply declare an one dimensional array of size 4.Then the Roll no and three subject marks can be stored in the array variable. But here the case is different. We have to store the values in the tabular form in a variable. Here comes the utility of two-dimensional array. We have to declare a 3 by 4 array.

#include < stdio.h >
void main ()
{
int result [3][4];
int i, j;
clrscr ();
for (i=0;i < 3;i++)

{
j=0;
printf ("\nEnter the Roll number:-");
scanf ("%d", &result [i][j]);
for (j=1;j < 4;j++)

{
printf ("\nEnter the marks of subject no.%2d\n", j);
scanf ("%d", &result [i][j]);
}
}
clrscr ();
printf ("The stored values are:\n");
for (i=0;i < 3;i++)

{
for (j=0;j < 4;j++)

{
printf ("%4d", result [i][j]);
}
printf ("\n");
}
getch ();
}


C Program on temperature of cities


The daily maximum temperature of 4 cities for 5 dates are recorded during the month of January. Write a program to find the day and city corresponding to highest temperature and lowest temperature

#include < stdio.h >
void main()
{
int temp[5][4],maxc,minc,maxd,mind,maxt,mint,i,j;
clrscr();
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
if(j==0)
printf("\nEnter temp. for Calcutta on %d January\t",i+1);
else if(j==1)
printf("\nEnter temp. for Madras on %d January\t",i+1);
else if(j==2)
printf("\nEnter temp. for Mumbai on %d January\t",i+1);
else if(j==3)
printf("\nEnter temp. for Delhi on %d January\t",i+1);
scanf("%d",&temp[i][j]);
}
}
clrscr();
puts("The recorded temperature:-\n");
puts("Calcutta Madras Mumbai Delhi");
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
printf("%10d",temp[i][j]);
}
printf("\n");
}
puts("**************");
for(i=0;i < 5;i++)

{
for(j=0;j < 4;j++)

{
if((i==0)&&(j==0))
{
maxt=temp[i][j];
maxd=i;
maxc=j;
mint=temp[i][j];
mind=i;
minc=j;
}
else
{
if(maxt < temp[i][j]) { maxt=temp[i][j]; maxd=i+1; maxc=j+1; } if(mint > temp[i][j])
{
mint=temp[i][j];
mind=i+1;
minc=j+1;
}
}
}
}
printf("\nThe maximum temperature is :\t%d",maxt);
printf("\nAnd the corresponding date is \t%d",maxd);
printf("\nCity number\t%d",maxc);
printf("\nThe minimum temperature is :\t%d",mint);
printf("\nAnd the corresponding date is \t%d",mind);
printf("\nCity number\t%d",minc);
getch();
}


program on student's result using 2 d array


The annual examination results of 10 students are tabulated as follows
Roll No. sub1 sub2 sub3
--------------------------------
Write a program to read data and determine the following
@Total marks obtain by each student
@The highest marks in each subject with Roll number
@The student who obtained the highest total marks

#include < stdio.h >

void main()
{
int result[10][5],i,j,k,total,maxr,maxm;
clrscr();
for(i=0;i < 10;i++)

{
total=0;
for(j=0;j < 4;j++)

{
if(j==0)
puts("Enter Roll number\t");
else if(j==1)
puts("Enter 1st subject marks\t");
else if(j==2)
puts("Enter 2nd subject marks\t");
else if(j==3)
puts("Enter 3rd subject marks\t");
scanf("%d",&result[i][j]);
if(j!=0)
total+=result[i][j];
}
result[i][j]=total;
}
clrscr();
puts("Mark sheet");
printf("\n%6s%6s%6s%6s%6s\n","Roll","1st","2nd","3rd","Total");
for(i=0;i < 10;i++)

{
for(j=0;j<5;j++)

{
printf("%6d",result[i][j]);
}
printf("\n");
}
puts("Press any key....");
getch();
puts("Roll number with total");
printf("\n%6s%6s\n","Roll","Total");
for(i=0;i < 10;i++)

{
for(j=0;j < 5;j++)

{
if((j==0)||(j==4))
printf("%6d",result[i][j]);
}
printf("\n");
}
for(i=1;i < 4;i++)

{
for(j=0;j < 10;j++)

{
if(j==0)
{
maxm=result[j][i];
maxr=result[j][0];
}
else if(result[j][i]>maxm)
{
maxm=result[j][i];
maxr=result[j][0];
}
}
if(i==1)
printf("\nMaximum marks in sub1 is %d, Roll number %d",maxm,maxr);
else if(i==2)
printf("\nMaximum marks in sub2 is %d, Roll number %d",maxm,maxr);
else if(i==3)
printf("\nMaximum marks in sub3 is %d, Roll number %d",maxm,maxr);
}
for(i=0;i < 10;i++)

{
for(j=0;j < 5;j++)

{
if(j==4)
{
if(i==0)
{
maxm=result[i][j];
maxr=result[i][0];
}
else if(result[i][j]>maxm)
{
maxm=result[i][j];
maxr=result[i][0];
}
}
}
}
printf("\nMaximum total is %d and Roll number %d",maxm,maxr);
getch();
}

To display a 5 by 5 array with the following output

Upper left traingle with +1
Lower left traingle with -1
Right to left diagonal with 0

#include < stdio.h >
void main()
{
int result[5][5],i,j;
clrscr();
for(i=0;i < 5;i++)

{
for(j=0;j < 5;j++)

{
if(i+j==4)
result[i][j]=0;
else if(i+j < 4) result[i][j]=1; else result[i][j]=-1; } } for(i=0;i < 5;i++)

{
for(j=0;j < 5;j++)

{
printf("%2d",result[i][j]);
}
printf("\n");
}
getch();
}


This page on 2-D Array using C Language. Any Question ? Put Your comments

Subscribe via email

Enter your email address:

Delivered by FeedBurner