• An arrayis a group of contiguous memory locations that all have the same type. • It help us to organize large amounts of information • AKA list, vector, single array, … etc. 1DA Definition
4.
Example: The ScoresArray The entire array has a single name Each value has a numeric index An array of size N is indexed from zero to N-1 This array holds 9 values that are indexed from 0 to8
5.
A particularvalue in an array is referenced using the array name followed by the index in brackets For example, the expression scores[2] refers to the value 12 (the 3rd value in the array) That expression represents a place to store a single integer and can be used wherever an integer variable can be used 1DA Definition
6.
For example,an array element can be assigned a value, printed, or used in a calculation: int first=3 scores[2] = 89; scores[first] = scores[first] + 2; mean = (scores[0] + scores[1])/2; printf ("Top = %d”, scores[5]); 1DA Definition
7.
The valuesheld in an array are called array elements An array stores multiple values of the same type (the element type) The element type can be a primitive type or a structure. Therefore, we can create an array of integers, or an array of characters, etc. 1DA Definition
Bounds Checking Oncean array is created, it has a fixed size An index used in an array reference must specify a valid element That is, the index value must be in bounds (0 to N-1) For example, if the array codes can hold 100 values, it can be indexed using only the numbers 0 to 99 If count has the value 100, then the following reference will cause a problem: for (int index=0; index <= 100; index++) codes[index] = index*50 + epsilon; problem
10.
1DA Initializing Wecan declare and initialize an array in one step The values are delimited by braces and separated by commas Examples: char letterGrades[ ] = {'A', 'B', 'C', 'D', ’F'};
11.
Initializer Lists Notethat when an initializer list is used: no size value is specified The size of the array is determined by the number of items in the initializer list An initializer list can only be used only in the array declaration.
Parallel Arrays Theseare independent arrays of the same size, that have a meaningful connection to each other. For example, one array with a students gpa, and one with his letter grade.
14.
• We typicallyuse for loops for any kind of array processing. • To input an array, one by one: for (i=0; i<10 ; i++ ) { printf(“ Enter element %d : “, i ); scanf ( “ %d “, &scores[i] ); } 1DA Input / Output
15.
• To displayan array, one element per line: for (i=0; i<10 ; i++ ) { printf(“ scores [%d] : %dn“, i , scores[i] ); } 1DA Output
16.
• double X[]= {16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, -54.5} • int i=5; • printf(“%f”, x[4]); • printf(“%f”, x[i]+1); • printf(“%f”, x[i+1]); • printf(“%f”, x[2*i]); • printf(“%f”, x[2*i-3]); • printf(“%f”, x[(int) x[4]]); • printf(“%f”, x[i++]); • printf(“%f”, x[--i]); • x[i]= x[i+1]; Sample Trace Problem Show the output of the following C-code segment:
17.
Sample Write Problems 1.Write a C-program that reads 5 student scores in order to compute their average: a. Don’t use loop neither array. b. Use loop but don’t use array. c. Use array but don’t use loop. d. Use array and loop 2. Write a C-program that reads a list of 5 student scores then call a function to compute their average. 3. Write a C-program that reads a list of 5 student scores then call a function to compute & display their average. 4. Write a C-program that call a function to reads a list of 5 student scores to display their average.
18.
#include <stdio.h> int main(void ) { //declaration part float score1, score2, score3, score4, score5, sum, avg; //input step printf(“score1= ”); scanf(“%f”, &score1); printf(“score2= ”); scanf(“%f”, &score2); printf(“score3= ”); scanf(“%f”, &score3); printf(“score4= ”); scanf(“%f”, &score4); printf(“score5= ”); scanf(“%f”, &score5); //calc step sum = score1 + score2 + score3 + score4 + score5 ; avg= sum/5; //output step printf( “Average of 5 scores=%fn", avg); return 0; } Solution-1a: No loop No array We used separate variable for each score
19.
#include <stdio.h> int main(void ) { //declaration part float score, sum, avg; //input & calc steps sum=0; for(int i=1; i<=5; i++) { printf(“score%d=”, i); scanf(“%f”, &score); sum = sum + score; } avg= sum/5; //output step printf( “Average of 5 scores= %fn", avg); return 0; } Solution-1b: loop but No array We used single variable to read all scores
20.
#include <stdio.h> #define SIZE5 int main( void ) { //declaration part float scoreArr[SIZE], sum, avg; //input step printf(“score1= ”); scanf(“%f”, &scoreArr[0]); printf(“score2= ”); scanf(“%f”, &scoreArr[1]); printf(“score3= ”); scanf(“%f”, &scoreArr[2]); printf(“score4= ”); scanf(“%f”, &scoreArr[3]); printf(“score5= ”); scanf(“%f”, &scoreArr[4]); //calc step sum = scoreArr[0] + scoreArr[1] + scoreArr[2] + scoreArr[3] + scoreArr[4] ; avg= sum/SIZE; //output step printf( “Average of %d scores=%fn", SIZE, avg); return 0; } Solution-1c: array but No loop We used separate variable for each score It is a good practice to define the array size as constant
21.
#include <stdio.h> #define SIZE5 int main( void ) { //declaration part float scoreArr[SIZE], sum, avg; //input & calc steps sum=0; for(int i=0; i<SIZE; i++) { printf(“score%d=”, i+1); scanf(“%f”, &scoreArr[i]); sum = sum + scoreArr[i]; } avg= sum/SIZE; //output step printf( “Average of %d scores= %fn", SIZE, avg); return 0; } Solution-1d: array & loop We used separate variable for each score
22.
#include <stdio.h> #define SIZE5 int main( void ) { //declaration part float scoreArr[SIZE], sum, avg; //input step for(int i=0; i<SIZE; i++) printf(“score%d=”, i+1); scanf(“%f”, &scoreArr[i]); //calc step sum=0; for(int i=0; i<SIZE; i++) sum = sum + scoreArr[i]; avg= sum/SIZE; //output step printf( “Average of %d scores= %fn", SIZE, avg); return 0; } Another Solution-1d: array & loop We used separate variable for each score
23.
#include <stdio.h> #define SIZE5 float avgFun(float arr[ ], int size); int main( void ) { //declaration part float scoreArr[SIZE], avg; //input step for(int i=0; i<SIZE; i++) printf(“score%d=”, i+1); scanf(“%f”, &scoreArr[i]); //calc step avg= avgFun(scoreArr, SIZE); //output step printf( “Average of %d scores= %fn", SIZE, avg); return 0; } float avgFun(float arr[ ], int size) { float sum=0; for(int i=0; i<size; i++) sum = sum + arr[i]; return sum/size; } Solution-2: only calc in function We used separate variable for each score Passing arrays to functions is always call by reference
24.
#include <stdio.h> #define SIZE5 void avgFun(float arr[ ], int size); int main( void ) { //declaration part float scoreArr[SIZE]; //input step for(int i=0; i<SIZE; i++) printf(“score%d=”, i+1); scanf(“%f”, &scoreArr[i]); //calc & output steps avgFun(scoreArr, SIZE); return 0; } void avgFun(float arr[ ], int size) { //calc step float sum=0; for(int i=0; i<size; i++) sum = sum + arr[i]; //output step printf( “Average of %d scores= %fn", size, sum/size); } Solution-3: calc & o/p in function We used separate variable for each score Passing arrays to functions is always call by reference
25.
#include <stdio.h> #define SIZE5 void avgFun( void ); int main( void ) { //input, calc & output steps avgFun( ); return 0; } void avgFun( void ) { //declaration part float scoreArr[SIZE]; //input step for(int i=0; i<SIZE; i++) printf(“score%d=”, i+1); scanf(“%f”, &scoreArr[i]); //calc step float sum=0; for(int i=0; i<SIZE; i++) sum = sum + scoreArr[i]; //output step printf( “Average of %d scores= %fn", SIZE, sum/SIZE); } Solution-4: i/p, calc & o/p in function We used separate variable for each score Writing void in parameter list is optional
26.
Sample Write Problems Write a program that reads in an array of 10 integers (range 0 - 50, strict) and then draws a bar chart of the values: 10 ********** 5 ***** 15 *************** ….
27.
Array searching Writea program that reads in an array of 50 numbers and then prints out their maximum. Solve again by using function to get the maximum Write a program that reads in the grades of 500 students in an exam and then display the success ratio. Write a program that reads in an array of 10 student grades, adds up 2 points to each grade (making sure no one goes over 100), and then tells you how many students got an A (over 85).
28.
Handling multiple arrays Write a program that reads in the id and grades of 30 students. Then it prints out the id of the highest scoring student. Write a program that reads in an array of grades for 30 students and creates another array for failed students only Write a program that creates 3 arrays for the id, price, and quantity of items being sold in a shop. The program prints the data in a table format.The program then prints out the ids of all items whose quantity is less than 10, and those items whose price is > 100.
29.
/* Reads datainto two arrays and subtract their corresponding elements, storing the result in another array. */ #include<stdio.h> #define SIZE 5 int main(void){ int first[SIZE], second[SIZE], diff[SIZE], i; // declaration part printf("Enter %d data items for first array : ", SIZE); for(i=0;i<SIZE; i++) // input first array scanf("%d",&first[i]); printf("Enter %d data items for second array : ", SIZE); for(i=0;i<SIZE; i++) // input second array scanf("%d",&second[i]); for(i=0;i<SIZE; i++) // calculate the differences diff[i]=second[i] - first[i]; printf("nnOutput of the arrays : n"); for(i=0;i<SIZE; i++) // output the arrays printf("%5d %5d %5dn", first[i], second[i], diff[i]); system("pause"); return 0; } example