C Programming Language - Arrays
C Programming Language - Arrays
Computer Programming
Arrays
Declaring Arrays
Array: uses consecutive area in memory
Can be referenced as a group
Simple memory types: single memory cell Group of related data items: adjacent memory cells
Ex:
double x[8];
Name of the array is x There are eight elements (memory cells) Each element is double
Declaring Arrays
Ex: double x[8]; Each element can be accessed individually Ex: x[0], x[1],, x[7]
printf(%.2f, x[0]); x[3] = 12.20; sum = sum +x[5]; x[2] = 13 + x[0]; x[7] = pow(x[1],x[4]); scanf(%lf, &x[0]);
GIT Computer Engineering Department
Parallel arrays
id[i] and gpa[i] are related
First students ID is in id[0] First students GPA is in gpa[0]
Declaring Arrays
More than one array can be declared at once double bolts[20], needle, pins[10]; An array can be initialized in declaration int primes[5] = {2, 3, 5, 7, 11}; int primes[] = {2, 3, 5, 7, 11}; Syntax: element_type array_name[size]; element_type array_name[size] = {initialization list};
Array Subscripts
Subscript specifies array elements
Any expression if type int Must be between 0 to size-1
Array Arguments
Passing whole arrays to functions
Array as a actual parameter
Ex: Fill an array with the same value void fill_array(int list[], int n, int in_value); fill_array(x, 5, 1)
GIT Computer Engineering Department
Function fill_array
Array Arguments
You can use *list instead of list[] in a formal parameter list Pass an array as a argument int list[]; means parameter is an array int *list; is correct as well
Array argument: passing the address of the first element But, it does not shot that the argument is an array! You should remember that it is array not output parameter
You can not return an array as a functions return value You should define it as a output parameter
Some part is reserved for later use Need to reuse the same array for other purpose later
Stacks
Remember stack?..
Only top element can be accessed Operations Push Pop Array as a stack What should be parameters to push and pop
Stacks
Remember stack?..
Only top element can be accessed Operations Push Pop Array as a stack
What should be parameters to push and pop void push(char stack[], char item, int *top, int max_size); char pop(char stack[], int *top); push(s, 2, &s_top, STACK_SIZE); c = pop(s, &s_top);
GIT Computer Engineering Department
Searching an Array
Searching: Locating a particular value Sorting: Ordering the elements
1. Assume the target has not been found 2. Start with the initial array element 3. Repeat while the target is not found and there are more array elements 4. if the current element matches the target 5. set a flag to indicate that target found else 6. Advance to the next array element 7. If the target was found 8. return the target index as the search result else return -1 as the search result
GIT Computer Engineering Department
Sorting an Array
Sorting is quite useful
Many operations implemented more efficiently if the data is sorted Output is more understandable if the information is sorted
Selection sort:
Not very efficient but simple Locate the smallest element and move it to location 0 Locate the smallest element in the remaining array starting with location 1 and move it to location 1 Locate the smallest element in the remaining array starting with location 2 and move it to location 2 Continue like this until location n-2
GIT Computer Engineering Department
1. for each value of fill from 0 to n-2 2. find index of the smallest element in the unsorted subarray list[fill] through list[n-1] 3. if fill is not the position of the smallest element 4. exchange the smallest element with the one at the position fill
Multidimensional Arrays
Tables of data Matrices Tic-tac-toe board char tictac[3][3];
Multidimensional Arrays
Syntax: element-type aname[size1][size2][sizen]; Parameter to a function element-type aname[][size2][sizen] Ex: double table[NROWS][NCOLS]; int tt[7][5][6]; void process_matix(double table[][NCOLS], int nrows); void process_t(int tt[][5][6], int nrows);
Find and display the total number of students in each course Find and display the number of students at each campus
Problem constants
NUM_UNITS NUM_QUARTERS 5 4
Problem inputs
Transaction file double revenue[NUM_UNITS][NUM_QUARTERS]
Problem outputs
double unit_totals[NUM_UNITS] double quarter_totals[NUM_QUARTERS]
GIT Computer Engineering Department
1. Scan revenue data, posting by unit and quarter, returning a value to show successor failure of the data scan 2. It the data scan proceeded without error 3. compute unit totals 4. compute quarterly totals 5. Display revenue table and row and column sums