Arrays_draft

# Arrays_draft - Single and Multidimensional Arrays Accident...

• Notes
• 42

This preview shows page 1 - 10 out of 42 pages.

Single and Multidimensional Arrays

Subscribe to view the full document.

Accident Problem Each year the Department of Traffic Accidents receives accident count reports from a number of cities and towns across the country. Given details of ‘n’ days, develop an algorithm and write a program to determine the average number of accidents and for each day, print the difference between the number of accidents on that day and average. For example, if the number of accidents is 5 and the values are 10, 12, 15, 13, 5 then average is 11 and the difference of values are 1, 1, 4, 2, 6
Accident problem Input Output Logic Involved Value of ‘n’, ‘n’ numbers Average and ‘n’ values that is the difference between average and value Find average and difference

Subscribe to view the full document.

Algorithm 1. Read the value of ‘n’ 2. Read the number of accidents happened in ‘n’ days 3. Find average 4. For each value print the difference between average and the value
New Stuff... We can find the difference between average and the number of accidents on a particular day only after reading all numbers from the user So data has to be stored Same type of data is to be stored Number of items not known prior Best choice would be using arrays in C Array - Can store a fixed-size sequential collection of elements of the same type

Subscribe to view the full document.

Arrays in C Consist of contiguous memory locations lowest address corresponds to the first element highest address to the last element Array indices start with zero The elements have indices from 0 to ‘n-1’ Similar to list in Python but homogenous
Declaration of Arrays in C type arrayName [ arraySize ]; double balance[10]; Initializing Arrays double balance[] = {1000.0, 2.0, 3.4, 7.0, 50.0}; (or) double balance[5] = {1000.0, 2.0, 3.4, 7.0, 50.0};

Subscribe to view the full document.

Difference between Initialization and Assignment Assignment int a; a = 5; Intialization int a = 5;

Subscribe to view the full document.

• Spring '16
• dncj ncnd
• Array, ASCII, D Arrays, Tic Tac Toe Board

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern