






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
grades, each of type float, we can declare our array as: float six_week_grade[ 5 ] ;. More generally, the syntax for declaring an array is.
Typology: Exams
1 / 12
This page cannot be seen from the preview
Don't miss anything!







(a) Describe how an array is stored in memory.
(b) Define a string, and describe how strings are stored.
(c) Describe the implications of reading or writing beyond the boundary of an array.
(d) Describe how to change the values of individual array elements.
1. Why Use Arrays?
Consider the following problem: Suppose an instructor has just finished grading the six-week exams for the twenty midshipmen in her section of EC310. Write a C program that will compute the average and also determine how far each student’s individual grade is from the average.
One way to start this program would be to declare twenty variables to hold the twenty grades:
float student1_grade, student2_grade, student3_grade … etc., etc.
This is cumbersome. All these variables! Ugh. Ain’t nobody got time for that! Suppose there were 100 students in the section. We would need 100 variables to hold the 100 grades.
Suppose we consider a different (better!) approach. Instead of having 20 separate variables to hold our 20 six- week exam scores, we will use a "large box with multiple slots." We might name the entire large box six_week_grade. The top slot will hold the first student’s six-week exam grade, the second slot will hold the second student’s six-week exam grade, and so forth. Instead of using the term "large box with multiple slots", let’s call this an array. In C, an array is frequently termed a buffer.
So that we can more easily draw our arrays, we will imagine that our section has five students. The concepts will, of course, apply to arrays of any size. Let's say, for the purposes of this discussion, that our five students: Mid 1, Mid 2, Mid 3, Mid 4 and Mid 5, have grades of 98 , 87.5 , 94 , 90 and 92 , respectively.
First student's grade goes here
Second student's grade goes here
Third student's grade goes here
2. Arrays An array is a collection of data, all of the same type. Recall that when we say type we are referring to int, float or char. More precisely, an array is a consecutive group of memory locations, all with the same name, all holding the same type of data. Our array of five student grades (all of type float) might be arranged in main memory as:
Notice first that our five array elements are stored in consecutive memory locations. Second, note that the addresses are separated by four bytes, since each value of type float is stored in four bytes. The precise location in main memory where the array will be stored is determined by the compiler.
You might be surprised to know that the third and most important point to note in the figure above are the ellipses (i.e., the three dots) shown at the top and bottom of the array! There are items in main memory "above" our array, and items in main memory "below" our array.
Suppose we want to give our array the name: six_week_grade. Recalling that our array will hold five grades, each of type float, we can declare our array as:
float six_week_grade[ 5 ] ;
More generally, the syntax for declaring an array is
type array_name [ number of items in the array ] ;
All items stored in the array Same rules as for Also called the " size " of the must be of the same type variable names array; must be an integer or an expression that evaluates to an integer.
The following would all be valid examples of array declarations:
float temperatures[31]; int calories[90]; float migraine_intensity_level[1000];
Returning to our example, when we declare our array as
float six_week_grade[ 5 ] ;
the compiler will reserve adjacent memory for five variables of type float. The entire array will be named six_week_grade. The picture would look like this (where the exact addresses are chosen by the compiler):
First student's grade goes here Second student's grade goes here Third student's grade goes here Fourth student's grade goes here Fifth student's grade goes here
The individual array elements are variables and can be used in expressions just as you would ordinarily use any variable. So, for example, the line of code
six_week_grade[ 2 ] = 94 ;
would change our picture to this:
So, the third midshipman grade, which we refer to as six_week_grade[ 2 ], is now stored in the array. (The other four array values are still garbage values.)
Important point guaranteed to cause confusion : Note that the first variable in the array has an index of zero , not one. This is counter-intuitive. In the above example, you would think that the first six-week exam grade should be indexed as six_week_grade[1]since, after all, it is the first score. You would be wrong! The first score in our array of scores is indexed as six_week_grade[0]. Most programming languages (e.g., C, C++, Java, JavaScript) start with the index at zero just to make it easier for the CPU to index into the array.
We could fill in our array in memory by adding the lines of code:
six_week_grade[ 0 ] = 98 ; six_week_grade[ 1 ] = 87.5 ; six_week_grade[ 3 ] = 90 ; six_week_grade[ 4 ] = 92 ;
after which our array is stored as shown:
It bears repeating: The individual array elements are variables and can be used in expressions just as you would ordinarily use any variable. If we wanted to add two points to the first midshipman's grade we could use
six_week_grade[ 0 ] = six_week_grade[ 0 ] + 2 ;
If we wanted to read the value of the third midshipman's grade in from the keyboard, we could use
scanf("%f", &six_week_grade[2] );
If we wanted to print the second midshipman grade to the monitor, we could use
printf("%f" , six_week_grade[1] );
We can use array elements in Boolean expressions, such as if ( score[3] > 90 )....
Note that when referring to an array element, the index does not need to be an integer constant. We can use any expression in the brackets that evaluates to an integer. As an example, the for loop below might be used to read in from the keyboard the grades for 5 students.
for ( number = 0 ; number < 5 ; number = number + 1 ) { printf( "Enter score for student %d : " , number + 1 ); scanf( "%f" , &six_week_grade[ number ] ); }
Practice Problem
Suppose we have 5 students in EC312. A portion of a C program that declares an array of floats named six_week_grade that will hold the midterm grades for the class is shown below. Your program should allow the user to enter the midterm grades at runtime, and should then print out the midterm grades. Your program output should appear as shown below:
Fill in the one missing line of code.
#include <stdio.h> int main() { float six_week_grade[5]; int number ; for ( number = 0 ; number < 5 ; number = number + 1 ) { printf( "Enter score for student %d : " , number + 1 ); scanf( "%f" , &six_week_grade[ number ] ); } for ( number = 0 ; number < 5 ; number = number + 1 ) {
After six pages of array syntax, you might be thinking: " Wonderful, so what ?" As we will see arrays have a horrendous security vulnerability baked into them.
5. The Dreaded Out-of-Range Error C will not prevent you from trying to access an array element that is out of the array's range. Stated another way, C will not prevent you from trying to read to or write to "nonexistent" array elements. What exactly does this mean? Consider the array declaration
int salary[3];
which declares an array with three variables: salary[0], salary[1] and salary[2]. But what happens if we have a statement such as
printf( “%d” , salary[3] );
when there is no variable salary[3]? Let's see! Consider the program below.
#include <stdio.h> int main() { int salary[3] = { 1000 , 1500 , 2000 }; int j; for ( j = 0 ; j <= 3 ; j = j + 1 ) { printf("Salary %d is %d \n" , j+1 , salary[j] ); } }
The output from this program is:
No compilation error results in the program above...but do you see the potential dangers? Where does the last number come from? The answer: It is the value that is located in memory immediately after salary[ 2 ]! This is a garbage value.
When we index an array variable using an index outside the range of indices specified in the array's declaration, we commit an " out-of-range error ." Again, it is critical to note that C will not prevent you from looking into memory beyond the end of your array.
What would be the danger in the following program snippet:
float salaries[3]; int j;
(other code not shown) for ( j = 0 ; j <= 3 ; j = j + 1 ) { printf("Enter salary %d: ", j + 1 ); scanf( "%f" , &salaries[j] ); } (more code)
The program above does not produce any compilation errors, but running this program is potentially very dangerous. Do you see why? Notice that the for loop, in its final iteration, attempts to enter a value into a variable named salaries[ 3 ]. There is no variable named salaries[ 3 ]! The program will simply write over whatever was stored in the memory location following salaries[ 2 ].
We now switch gears (slightly) and talk about arrays of characters—that is, arrays where each element in the array is of type char.
1. Introduction Suppose the contents of 5 consecutive bytes in computer memory are as follows:
0100 1110 0110 0001 0111 0110 0111 1001 0000 0000
As ASCII characters, this is the same as:
'N' 'a' 'v' 'y' 0
With other types, such as int, float and char, you were allowed to assign values to the variables in a manner like this:
int favorite_number ; favorite_number = 7 ;
Unfortunately, assignments cannot be done like this with strings. The code below will not compile:
char school[5]; school = "Navy";
So… how can we change the value of a string? There are two ways.
A. Changing a string value character by character. We can change the value of a string by changing the values of the individual characters. For example: char school[5] = "Navy"; printf( "%s\n", school ); school[0] = 'U'; school[1] = 'S'; school[2] = 'N'; school[3] = 'A'; printf( "%s\n" , school );
Practice Problem
Continuing the example above, what would happen if we modified two lines of code as shown below:
school[2] = 'A'; school[3] = 0 ; printf( "%s\n", school ); Solution
B. Changing a string value using strcpy The second way to change a string’s value is with the “string copy” function. The syntax is
strcpy( s1 , s2 );
This function copies the string s2 to the string s1. When the string s2 is copied over the string s1, the strcpy function automatically places a closing NULL at the end of the new (modified) string s1.
To use the function strcpy, you must have the following line at the top of your program:
#include<string.h>
One final note about strings. When entering strings from the keyboard don’t use the ampersand: &. For example, to enter a midshipman's last name from the keyboard, we would use: char mid_name[24] ; scanf( "%s " , mid_name );
Practice Problem
We want to write a C program that declares a string (a character array) and initializes it to "Military Academy", prints this string to the screen, then, within the program, changes the string to the name of your favorite college, and then, once again, prints the string to the monitor.
Your program output should appear as shown below:
Fill in the three missing lines of code.
Solution:
#include <stdio.h> #include<string.h> int main() {
char phrase[] = "Military Academy" ;
" It came from California… the students were safe… their computers weren't … " http://www.youtube.com/watch?v=G2i_6j55bS
What's that you say? You don't see the danger!. Let's watch another video.
https://youtu.be/Ar-l3FRUdGw
Practice Problem