Monday, April 25, 2016

Arrays (1D)




Image result for Arrays





An array is a container object that holds a fixed number of values of a single type. The length of an array is established when thearray is created. After creation, its length is fixed.






Example 1: (scan & print ) Implement a C code that take 10 students degrees and print them by using Array method .

Code : 


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float students_degree[10];
    int i ;
    printf("please enter 10 students degrees\n");
    for (i=0;i<10;i++){

        scanf("%f",&students_degree[i]);

    }
     for (i=0;i<10;i++){


        printf("%f\n",students_degree[i]);

     }

    return 0;
}

Output 1 :





Example 2 : Implement a C code to calculate F(x)=x^3 +2x+7.6x+3 by using Array method.

Code :


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float x[5]={6.2,7.3,5,2.66,0.566};
    int i ;
    float y ;
    for (i=0;i<5;i++){
   y=(x[i]*x[i]*x[i])+(2*x[i]*x[i])+(7.6*x[i])+3;
   printf("y<%f>=%f\n",x[i],y);
    }

    return 0;
}



Output 2 : 





Example 3 : Calculate the maximum of Array 

Code :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdlib.h>

int main()
{
    float x[5];
    int i ;
    float max=0 ;


    for (i=0;i<5;i++){
        scanf("%f",&x[i]);
     }

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

    if(max<x[i]){

    max=x[i];}

    }
    printf("The maximum = %f \n",max);

    return 0;
}


Output 3 :










Share this

0 Comment to "Arrays (1D)"

Post a Comment