Sunday 24 July 2016

Data strucutres : Assignment 1

Hello guys and girls !
This an humble attempt to learn, share, and help !
I hope this will be helpful to you . Please refer this/ check this or if there is error suggest modification/ better algorithms .

Question : 1

/*

Array operation by index
Q.1 Write a program to calculate sum of array and find the average.
*/


#include<stdio.h>

float main()
{
int n,i;
float avg,sum=0.0;
printf("Enetr no of elements:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
    scanf("%d",&array[i]);
    sum += array[i];
}
avg = sum/n;
printf("The average is %4.2f \n",avg);
}


/*
Array operation by address
Q.1 Write a program to calculate sum of array and find the average.
*/


#include<stdio.h>

float main()
{
int n,i;
float avg,sum=0.0;
printf("Enetr no of elements:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements:\n");
for(i=0;i<n;i++)
{
    scanf("%d",array+i);
    sum += *(array+i);
}
avg = sum/n;
printf("The average is %4.2f \n",avg);
}




Question : 2

/*
Array operation by index
Q 2 Array contains age of different person.  write a program to find max age and min age from array.
*/

#include<stdio.h>
void main()
{
int n,i,j,k,maxAge=0,minAge=1000;
printf("Enter the number of person:\n");
scanf("%d",&n);
int age[n];
printf("Enter the age:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",&age[i]);

        if( age[i] < minAge )
        {
            minAge = age[i];
        }
        if( age[i] > maxAge )
        {
            maxAge = age[i];
        }
    }
printf("\nMax age : %d \n",maxAge);
printf("Min age : %d \n",minAge);
}

/*
Array operation by address
Q 2 Array contains age of different person.  write a program to find max age and min age from array.
*/

#include<stdio.h>
void main()
{
int n,i,j,k,maxAge=0,minAge=1000;
printf("Enter the number of person:\n");
scanf("%d",&n);
int age[n];
printf("Enter the age:\n");
    for(i=0;i<n;i++)
    {
        scanf("%d",(age+i));

        if( *(age+i) < minAge )
        {
            minAge = *(age+i);
        }
        if( *(age+i) > maxAge )
        {
            maxAge = *(age+i);
        }
    }
printf("\nMax age : %d \n",maxAge);
printf("Min age : %d \n",minAge);
}







Question : 3

/*
accessing array by index
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

max=max2 = array[0];
min=min2= largest_value;
for(i=0;i<n;i++)
{
    if(array[i]<min)
    {
    min=array[i];
    }

    if(array[i]>max)
    {
    temp2=max;
    max=array[i];
    max2=temp2;
    }
}
for(i=0;i<n;i++)
{ if(array[i]<min2 && array[i]!=min)
    min2=array[i]; }

printf("2nd max %d \n2nd min %d \n",max2,min2);
}

/*
accessing array by address
Q3 Find second largest and second smallest element from array.
*/

#include<stdio.h>
#define largest_value 1000
int minmax2();
int main()
{
int i,j,k,n;
int max,min,max2,min2,temp1,temp2;
printf("Enter size of array :\n");
scanf("%d",&n);
int array[n];
printf("Enter the elements of array:\n");

for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minmax2(array,n,&min,&min2,&max,&max2);
}

//function

int minmax2(int *array,int n,int *min,int *min2,int *max,int *max2)
{
    int i,temp1,temp2;
    *max=*max2 = *array;
    *min=*min2= largest_value;
    for(i=0;i<n;i++)
    {
    if(*(array+i)<*min)
    {
    *min=*(array+i);
    }

    if(*(array+i)>*max)
    {
    temp2=*max;
    *max=*(array+i);
    *max2=temp2;
    }
    }
    for(i=0;i<n;i++)
    { if(*(array+i)<*min2 && *(array+i)!=*min)
    *min2=*(array+i); }

    printf("2nd max %d \n2nd min %d \n",*max2,*min2);
}


Question : 4

/*
accessing array by index
Q4 Add an element in array at particular index given by user.
*/

#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

//getting the index and value

printf("Enter the index:\n");
scanf("%d",&index);

printf("Enter element:\n");
scanf("%d",&new);

// inserting the element
// shifting elements upper than the index to next
    for(i=n-1;i>=index;i--)
    {
        array[i+1] = array[i];
    }
array[index] = new;
printf("Here is the desired array:\n");
    for(i=0;i<=n;i++)
    {
        printf("array[%d] = %d \n",i,array[i]));
    }
}


/*
accessing array by address
Q4 Add an element in array at particular index given by user.
*/

#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",array+i);
}

//getting the index and value

printf("Enter the index:\n");
scanf("%d",&index);

printf("Enter element:\n");
scanf("%d",&new);

// inserting the element
// shifting elements uper than the index to next
    for(i=n-1;i>=index;i--)
    {
        *(array+i+1) = *(array+i);
    }
*(array+index) = new;
printf("Here is the desired array:\n");
    for(i=0;i<=n;i++)
    {
        printf("array[%d] = %d \n",i,*(array+i)));
    }
}





Question : 5

/*
Accessing array elements by index
Q5 delete an element in an array at particular index given by user
*/
#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}

//deleting the element

printf("Enter the index:\n");
scanf("%d",&index);
for(i=index;i<n-1;i++)
{
array[i]=array[i+1];
}
printf("The desired array is:\n");
for(i=0;i<n-1;i++)
{
printf("array[%d] = %d\n",i,array[i]);
}
}


/*
Accessing array elements by address
Q5 delete an element in an array at particular index given by user
*/
#include<stdio.h>

#define maxSize 100

void main()
{
int i,n,index,new,array[maxSize];
printf("Enter the size of array:\n");
scanf("%d",&n);
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",array+i);
}

//deleting the element

printf("Enter the index:\n");
scanf("%d",&index);
for(i=index;i<n-1;i++)
{
    *(array+i)=*(array+i+1);
}
printf("The desired array is:\n");
for(i=0;i<n-1;i++)
{
printf("array[%d] = %d\n",i,*(array+i));
}
}






Question : 6

/*
Q6 Write a function which return min and max value from an array
*/
#include<stdio.h>
int min(int *array,int n);
int max(int *array,int n);
void main()
{
int i,n,minValue,maxValue;
printf("Enter the size of array:\n");
scanf("%d",&n);
int array[n];
printf("Enter array elements :\n");
for(i=0;i<n;i++)
{
scanf("%d",&array[i]);
}
minValue = min(array,n);
maxValue = max(array,n);
printf("The min :\t%d\nThe max :\t%d\n",minValue,maxValue);
}

//function for minimum of an array
int min(int *array,int n)
{
int i;
int minimum = *(array);
for(i=0;i<n;i++)
{
    if(*(array+i)<minimum)
    {
    minimum = *(array+i);
    }
}
return minimum;
}


//function for maximum of an array
int max(int *array,int n)
{
int i;
int maximum = 0;
for(i=0;i<n;i++)
{
    if(*(array+i)>maximum)
    {
    maximum = *(array+i);
    }
}
return maximum;
}



Question : 7

/*
Q 7
Write a program to add weight
create a structure for weight which contains Kg and g
*/
#include<stdio.h>
#define conversion 1000
struct weight{
int kg,gram;
};


main()
{
struct weight w1,w2;
int grandKg,grandGram;
printf("Enter weight 1 :\n");
printf("kg :\t");
scanf("%d",&w1.kg);
printf("\ngram :\t");
scanf("%d",&w1.gram);


printf("Enter weight 2 :\n");
printf("kg :\t");
scanf("%d",&w2.kg);
printf("\ngram :\t");
scanf("%d",&w2.gram);

grandKg = w1.kg + w2.kg ;

grandGram = w1.gram + w2.gram;

if(grandGram >= conversion )
{
grandKg += 1;
grandGram -= conversion ;
}

printf("total weight kg: %d gram %d  :\n",grandKg,grandGram);


}


/*
Q 7
Write a program to add weight using pointer .
create a structure for weight which contains Kg and g
*/


#include<stdio.h>

struct weight {
    int kg;
    int grams;
};
typedef    struct weight w;
void  add(w *w1,w *w2);

void main()
{

  
   w w1,w2;
 
  
   printf("enter the weight1 with kg and its grams\n");
   scanf("%d%d",&w1.kg,&w1.grams);
    printf("enter the weight2 with kg and its grams\n");
   scanf("%d%d",&w2.kg,&w2.grams);
  
   add(&w1,&w2);
 
  
  
  
  
}
void  add(w *w1,w *w2)
{
    w p;
    p.kg= w1->kg + w2->kg;
    if((w1->grams)+(w2->grams)>=1000)
    {
        p.grams=(w1->grams)+(w2->grams)-1000;
        p.kg++;
    }
    else
    {
        p.grams=w1->grams+w2->grams;
    }
    printf("the total weight is %dkg %dgrams",p.kg,p.grams);
}







Thanking you !

No comments:

Post a Comment