-->

lab work 4(array)

 

Lab Work- 4 : Array

  1. Write a C program to input marks of five subjects and display the total marks and average marks.

  2. Write a C program to read the age of 100 persons and count the number of persons in the age group between 50 to 60 years.

  3. Write a C program to input 'n' numbers and display the sum of even and odd numbers respectively.

  4. Write a C program to input 'n' numbers and find out the greatest, smallest and average number.

  5. Write a C program that takes salary of 100 employees and print the salary of the employees in ascending order.

  6. Write a C program to transpose the mxn matrix.

  7. Write a C program to  calculate the sum of diagonal matrix.

  8. Write a C program to add two matrices supplying by elements by the user.

-------------------------------------------------------------------------------------------------------------------------------------------
solutions:-
q1)Write a C program to input marks of five subjects and display the total marks and average marks.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array marks[5] of integer type
step 3:declare a variable i of integer type
step 4:set total=0
step 4:
        execute the loop from i=0 to 4
        input marks[i]
        total=total+marks[i]
        end of loop
step 5:
        let average=total/5
step 6: print total,average
step 7:stop
/*program to input marks
of five subjects and display the total marks and average marks.
*/
#include<stdio.h>
int main()
{
int marks[5],total=0;
float average;

for(i=0;i<5;i++)
{
   printf("enter marks for %d subject\n",i);
   scanf("%d",&
marks[i]);
total=total+marks[i];
}

average=(float)total/5;

printf("total marks=%d,average marks=%f\n",total,average);
return 0;
}

Q2)Write a C program to read the age of 100 persons and count the number of persons in the age group between 50 to 60 years.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array age[100]
step 3:declare a variable i of integer type
step 4:set total_age=0
step 5:
        execute the loop from i=0 to 100
        input age[i]
            if age[i]>=50 and age[i]<=60
               total_age=total_age+1
            end of if
        end of loop

step 6: print total_age
step 7:stop
"C" code:

/* program to store age of 'n' persons and count
the number of persons in the age group between 50 to 60 years.
*/
#include <stdio.h>

int main()
{
    int age[100],count=0,i;
    printf("enter persons age\n");
   for(i=0;i<100;i++)
    {
        printf("enter age for %d person\n",i+1);
        scanf("%d",&age[i]);
    }
    for(i=0;i<100;i++)
    {
          if(age[i]>50 && 
age[i]<60 )
                {
                   count++;
                 }
    }
      printf("total persons in range 50 and 60=%d\n",count);
    return 0;
}

q3)Write a C program to input 'n' numbers and display the sum of even and odd numbers respectively.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array numbers[100]
step 3:declare a variable i,size_array of integer type
step 4:set sum_even=0,sum_odd=0
step 5:input size_array
step 6:
        execute the loop from i=0 to size_array-1
        input numbers[i]
            let remainder=numbers[i]/2
                if remainder=0               
                    sum_even=sum_even+numbers[i]
                else
                    sum_odd=sum_odd+numbers[i]
                end of if
        end of loop

step 7: print sum_even,sum_odd
step 8:stop
// program to get sum of  even and odd numbers among 'n' numbers

#include <stdio.h>

int main()
{
    int a[100],n,i,sum_even=0,sum_odd=0;
    printf("enter total number\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("enter number for location=%d",i);
        scanf("%d",&a[i]);
   
      if(a[i]%2==0)      
      { 
      sum_even=sum_even+a[i];
       }
else
{
sum_odd=sum_odd+a[i];
}
}
    printf("sum of even =%d,sum of odd numbers=%d\n",
sum_even,sum_odd);
    return 0;
}
q4)Write a C program to input 'n' numbers and find out the greatest, smallest and average number.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array numbers[100]
step 3:declare a variable i,size_array,average
step 4:set greatest=0,smallest=0,sum=0
step 5:input size_array
step 6:
        execute the loop from i=0 to size_array-1
            input numbers[i]  
            sum=sum+numbers[i] 
        end of loop
step 7: let greatest=numbers[0],smallest=numbers[0]
step 8:
        execute the loop from i=0 to size_array-1
            if greatest< numbers[i]  
               greatest=numbers[i]
            else if smallest> numbers[i]  
               smallest=numbers[i]
            end of if
        end of loop
step 9: average=sum/size_array
step 10:print greatest,smallest,average
step 11:stop
//program to get maximum and minimum value
#include<stdio.h>

int main()
{
int a[200],size,i,max,min,sum=0;
float average;
printf("enter value of size to be stored in array\n");
scanf("%d",&size);
for(i=0;i<size;i++)
{
   printf("enter value for location=%d",i);
   scanf("%d",&a[i]);
sum=sum+a[i];
}
max=a[2];
min=a[1];
for(i=0;i<size;i++)
{
   if(max<a[i])
     {
       max=a[i];
     }
 else if (min>a[i])
   {
     min=a[i];
    }
}
average=(float)sum/size;
printf("the maximum value=%d\n",max);
printf("the minimum value=%d\n",min);
printf("aveage value=%f\n",average);
return 0;
}

q5)Write a C program that takes salary of 100 employees and print the salary of the employees in ascending order.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array salary[100]
step 3:declare a variable i,j,k of integer type

step 4:
        execute the loop from i=0 to 99
            input salary[i]
        end of loop

step 5:        
        execute the loop from i=0 to 99
            execute the loop from j=i+1 to 99
                    if salary[i]>salary[j]
                        k=salary[i]
                        salary[i]=salary[j]
                        salary[j]=k
                    end of if
            end of loop
        end of loop
step 6:
        execute the loop from i=0 to 99
            print salary[i]
        end of loop

step 7:stop
//program to sort 'n' employees salary in ascending order
  #include<stdio.h>
 
  int main()
  {

  int salary[100];       
  int i,j,k;      
 
printf("enter employees salary\n");                 
  for(i=0;i<100;i++)
   {                            
     printf("enter salary  for %d person\n",i+1);
     scanf("%d",&
salary[i]);      
   }
   
   for(i=0;i<100;i++)
   {
       for(j=i+1;j<100;j++)
        {
           if(salary[i]>salary[j])
             {
                 k=salary[i];
                 salary[i]=salary[j];
                salary[j]=k;
             }
       }
}
printf("in ascending order,salary are\n");
for(i=0;i<100;i++)
   {                            
     printf("%d\n",
salary[i]);      
   }
return 0;
}

q6)Write a C program to transpose the mxn matrix.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array matrix[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:input row,col
step 6:
        execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix[i][j]
            end of loop
        end of loop

step 7:        
         execute the loop from i=0 to col-1
            execute the loop from j=0 to row-1            
                print matrix[j][i]
            end of loop
        end of loop
step 8:stop
code:-
//WAP to transpose  a matrix/array of order mxn.
#include<stdio.h>
int main()
{
int matrics_1[100][100];
int i,j;
int total_row,total_col;
printf("enter total number of rows(m)\n");
scanf("%d",&total_row);                     // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col);                     // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)                  // nested loop for input of elements
       {
           scanf("%d",&matrics_1[i][j]);   // getting inputs from user
        }
}
printf("before transpose\n");             // message display
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)             
       {
           printf("%d",matrics_1[i][j]); // display of matrix
        }
        printf("\n");                    // display in next row
}
printf("after transpose\n");             // for transposed matrix
for(i=0;i<total_col;i++)
{
  for(j=0;j<total_row;j++)
       {
        printf("%d",matrics_1[j][i]);    // display of matrix with trasnposed elemnts
       }
       printf("\n");
}


 return 0;       
}

q7)Write a C program to  calculate the sum of diagonal matrix.
Ans:-
Algorithm
step 1: start
step 2:Declare an array matrix[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:set sum=0
step 6:input row,col
step 7:
   if row=col   
        print the addition of diagonal elements is possible
        print enter the elements
         execute the loop from i=0 to row-1
                execute the loop from j=0 to col-1            
                    input matrix[i][j]
                        if i=j
                            sum=sum+matrix[i][j]
                        endif
                end of loop
         end of loop
    else
        print addition of diagonal elements for given matrix can not be done
    endif
step 8:        
        print sum
step 9:stop

code:
//WAP to get sum of diagonal elements of an array of order 'mxn'.
#include<stdio.h>
int main()
{
int matrics_1[100][100];
int i,j,sum=0;
int total_row,total_col;
printf("for sum, the matrix must be square\n");
printf("enter total number of rows(m)\n");
scanf("%d",&total_row);                     // for total number of rows of matrix
printf("enter total columns(n)\n");;
scanf("%d",&total_col);                     // for total number of columns of matrix
printf("enter elements of matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)                  // nested loop for input of elements
       {
           scanf("%d",&matrics_1[i][j]);   // getting inputs from user
        }
}
printf("the matrix is\n");             // message display
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)             
       {
           printf("%d",matrics_1[i][j]); // display of matrix
        }
        printf("\n");                    // display in next row
}
printf("------------------\n");             // for transposed matrix
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           if(i==j)                          // testing whether the locations are same or not
           {
           sum=sum+matrics_1[i][j];
            }                                // display of matrix with trasnposed elemnts
       }
       printf("\n");
}

printf("sum=%d",sum);
 return 0;       
}

q8)Write a C program to add two matrices supplying by elements by the user.
Ans:-
Algorithm:
step 1: start
step 2:Declare an array matrix1[100][100],matrix2[100][100]
step 3:declare variables row,col
step 4:declare variables i,j of integer type
step 5:input row,col
step 6:
       print enter elements of first matrix
         execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix1[i][j]
            end of loop
        end of loop
step:7
      print enter elements of second matrix  
        execute the loop from i=0 to row-1
            execute the loop from j=0 to col-1            
                input matrix2[i][j]
            end of loop
        end of loop
step 8:        
         execute the loop from i=0 to col-1
            execute the loop from j=0 to row-1            
                print matrix1[j][i]+matrix2[j][i]
            end of loop
                print goto next line
        end of loop
step 9:stop

code:

///program to get sum of two matrices of order mxn
#include<stdio.h>
#include<conio.h>
void main()
{
int matrics_1[100][100],matrics_2[100][100];
int i,j;
int total_row,total_col;
printf("enter total number of rows(m)\n");
scanf("'%d",&total_row);
printf("'enter total columns(n)\n");;
scanf("%d",&total__col);
printf("enter elements of first matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_1[i][j]);
        }
}
printf("now enter for second matrix\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           scanf("%d",&matrics_2[i][j]);
        }
}
 printf("the summed matrix is\n");
for(i=0;i<total_row;i++)
{
  for(j=0;j<total_col;j++)
       {
           printf("%d",matrics_1[i][j]+matrics_2[i][j]);
        }
      printf(\n");
}
getch();
}

No comments:

Post a Comment