printfnThe s um o f firs t thre e array e le m e nts is d to tal end of main

Printfnthe s um o f firs t thre e array e le m e nts

This preview shows page 3 - 6 out of 8 pages.

printf("\nThe  s um  o f firs t thre e  array e le m e nts  is   = % d", to tal); // end of main int sum_ele m e nts(int  a, int b,  int c)    // Function header { int sum; sum=a+b+c; return (sum); } Sample Output: Solved Problem# 2: // Reads a 1-D array and prints the sum of even and odd numbers #include <stdio.h> #define SIZE 4 /* Determines if an element is odd */ int odd(int num) {      int isodd; isodd = (num%2==1);    return isodd; //end of odd function int main() {  int x[SIZE], i, oddsum=0, evensum=0;   printf("Enter %d integer values :", SIZE); ICS 103 : Computer Programming in C , Term-032 ; Rafiq Khan Page 3 of 8
Image of page 3
for (i=0; i<SIZE; i++) { scanf("%d", &x[i]); } for (i=0; i<SIZE; i++) { if ( odd(x[i]) ) // function call is with in if oddsum=oddsum+x[i]; else evensum=evensum+x[i]; } printf("The sum of odd elements is : %d\n", oddsum); printf("The sum of even elements is : %d\n", evensum); return 0; } // end of main Sample Output: How to pass whole array as a parameter in Function? : Unlike simple variables, array is not passed into a function, but rather its address is passed. This is done by specifying the array name followed by brackets [] (size is not necessary). This makes processing more efficient. The actual parameter is the array name ( no brackets ). ICS 103 : Computer Programming in C , Term-032 ; Rafiq Khan Page 4 of 8
Image of page 4
Solved Problem#3: /* reads the grades of students and prints the average grade.*/ #include <stdio.h> #define SIZE 30 /* computes the average of elements in an array */ float average(float list[], int n) { int i;    float sum=0.0;    for (i=0; i<n; i++)       sum+=list[i];    return sum/n; // end of function average int main() { int i,n; float grades[SIZE];  // array declaration   printf("Enter number of students: ");   scanf("%d",&n);   printf("Enter grades for %d students :", n);   for (i=0; i<n; i++)   {
Image of page 5
Image of page 6

You've reached the end of your free preview.

Want to read all 8 pages?

  • Spring '10
  • zaman
  • Array, Subroutine, #define, Even and odd functions

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes