Adds two arrays and return the result in another array include stdioh define

Adds two arrays and return the result in another

This preview shows page 28 - 33 out of 46 pages.

Adds two arrays and return the result in another array */ #include <stdio.h> #define SIZE 5 void read_array(double a[], int size); void print_array(double a[], int size); void add_arrays(double a[], double b[], double c[], int size); int main(void) { double first[SIZE], second[SIZE], sum[SIZE]; read_array( first , SIZE); read_array( second , SIZE); add_arrays( first , second , sum , SIZE); printf("First Array: "); print_array( first , SIZE); printf("Second Array: "); print_array( second , SIZE); printf("Sum of Arrays: "); print_array( sum , SIZE); system("pause"); return 0; } 28 void read_array (double a[], int size) { int i; printf("Enter %d integer numbers separated by blanks\n> ", size); for (i = 0; i < size; ++i) scanf("%lf", &a[i]); } void add_arrays(double a[], double b[], double c[], int size) { int i; for (i=0; i<size; i++) c[i] = a[i] + b[i]; } void print_array(double a[], int size) { int i; for (i = 0; i < size; ++i) printf("%.2f\t", a[i]); printf("\n"); }
Image of page 28
PARTIALLY FILLED ARRAYS The format of array declaration requires that we specify a size at the point of declaration. Moreover, once we decide on a size and declare the array, the size cannot be changed – array is fixed size data structure. There are many programming situations where we do not really know the number of elements before hand. For example, suppose we wish to read scores of students from a data file, store them into an array and then scan through the array to find the average. Obviously, we do not know how many scores are in the file. So what should be the array size? One solution is to declare the array big enough so that it can work in the worst-case scenario. For the scores data file, we can safely assume that no section is more than 50 students. However, in this case, the array will be partially empty and we cannot use SIZE in processing it. We must keep track of the actual elements in the array using another variable. 29
Image of page 29
EXAMPLE 13 /* Finds the average score by reading scores from a data file */ #include <stdio.h> #define SIZE 50 double get_average(double a[], int size); int main(void) { double x[SIZE], score, average; int status, count=0 ; FILE *infile; infile = fopen("scores.txt", "r"); status = fscanf(infile, "%lf", &score); while (status != EOF) { x[count] = score; count++; status = fscanf(infile, "%lf", &score); } fclose(infile); average = get_average( x , count ); printf("The average of the scores in the file is %.2f\n", average); system("pause"); return 0; } 30 double get_average(double a[], int size) { int i; double sum = 0; for (i = 0; i < size; ++i) sum += a[i]; return sum/size; }
Image of page 30
SEARCHING Searching means scanning through a list of items (in an array) to find if a particular one exists. It usually requires the user to specify the target item – the item he wishes to locate If the target item is found, its location (index) is returned, otherwise, -1 is returned. 31
Image of page 31
LINEAR SEARCH ALGORITHM This involves searching through the array sequentially until the target item is found or the array is exhausted.
Image of page 32
Image of page 33

You've reached the end of your free preview.

Want to read all 46 pages?

  • Summer '14
  • Darwish
  • Array, scanf, Array Subscripts

  • 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