CSCE 206 Lab Structured Programming in C SPRING 2019 Lecture 6
Arrays Store many items in the same variable Fixed size: specified at declaration 1 2 3 5 8 13 21 34 55 Value: Position: 9
Arrays Store many items in the same variable Fixed size: specified at declaration 1 2 3 5 8 13 21 34 55 Segmentation fault Value: Position: 10
Array declaration & use int fib[10]; fib[0] = fib[1] = 1; for (i = 2; i < 10; i++) { fib[i] = fib[i-1] + fib[i-2]; } for (i = 0; i < 10; i++) { printf("%d\n", fib[i]);
Array in functions (pass by reference) int fib[10]; fib[0] = fib[1] = 1; fibonacci(fib); for (i = 0; i < 10; i++) { printf("%d\n", fib[i]); }
Array in functions (pass by reference) void fibonacci(int fib){ for (i = 2; i < 10; i++) { fib[i] = fib[i-1] + fib[i-2]; }
Practice Problem-1 Write a program that takes from the input: One integer “size” that represents the size of the array “size” number of integers (int array) And prints the sum and the product of all the numbers From the main, call: One function to get the input from the array (not the size) One function to calculate the product One function to calculate the sum
Practice Problem-2 Write a program that takes from the input: One integer “size” that represents the size of the array “size” number of integers (int array) And prints the series of numbers inputted, each incremented by 1 From the main, call: One function to get the input from the array (not the size) One function to modify the original array One function to print the array
Practice Problem-3 Write a program that takes from the input: One integer “size” that represents the size of the array “size” number of characters (char array) And prints the characters in reversed order From the main, call: One function to get the input from the array (not the size) One function to modify the original array One function to print the array