0% found this document useful (0 votes)
16 views23 pages

Lecture 18

Uploaded by

nasha ajawahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views23 pages

Lecture 18

Uploaded by

nasha ajawahk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

ARRAYS

Lecture 18
The variables we have discussed so far:
–int, float, char etc.
–Store one value at a time
–If we store a new value, the old value is replaced
–Would it be better if a variable could store more than one
values at a time ?

• An array is a group (or collection) of same data


types. For example an int array holds the elements
of int types while a float array holds the elements
of float types and similarly for the character array.
Why we need Array in C Programming?

• Consider a scenario where you need to find out


the average marks obtained by a class of 100
students in a test. In C, you have two ways to do
this: 1) Define 100 variables with int data type and
then perform 100 scanf() operations to store the
entered values in the variables and then at last
calculate the average of them. 2) Have a single
integer array to store all the values, loop the array
to store all the entered values in array and later
calculate the average.
Which option is better?
Obviously, the second alternative is better. A simple reason for
this is, it would be much easier to handle one variable than
handling 100 different variables.
• An array is nothing more than a bunch of variables.
Each variable has the same name (the array name).
You distinguish among the variables in the array (the
array elements) with a numeric subscript. The first
array element has a subscript of 0, and the rest count
up from there.
Arrays are characterized by brackets that follow the array
names. i.e int num[35]; /* An integer array of 35 elements
*/
The array subscripts go inside the brackets when you need
to refer to an individual array element.
• Use arrays to hold lists of values of the same data type.
• Refer to the individual elements of an array with a
subscript.
• Write for loops if you want to “step through” every array
element, whether it be to initialize, print, or change the
array elements.
• In an array, don’t use more elements than defined
subscripts.
• Don’t use an array until you have initialized it with values.
• All arrays contain values called elements. An
array can contain only elements that are of the
same type.
• In other words, you can’t have an array that
has a floating-point value, a character value,
and an integer value.
• You define arrays almost the same way you
define regular non-array variables. To define a
regular variable, you only have to specify its
data type next to the variable name:
• int i; /* Defines a non-array variable */
To define an array, you must add brackets ([])
after the name and specify the maximum
number of elements you will ever store in the
array:
• int i[25]; /* Defines the array */
Lets declare an array
• Int grades[5]
It defines an array of int type which has 5
elements

If you want to initialize a character array with an


initial string, you know that you can do this:
• char name[6] = "Italy"; /* Leave room for the
null! */
various ways to initialize arrays
• If the initial array needs to be larger than the
initial value you assign, specify a larger array
size when you define the array, like this:
char name[80] = "Italy"; /* Leaves lots of extra
room */
• Doing this makes room for a string much
longer than Italy if you want to store a longer
string in name.
• the following statement both defines an
integer array and initializes it with five values:
• int vals[5] = {10, 40, 70, 90, 120};
• float money[10] = {6.23, 2.45, 8.01, 2.97,
6.41};
• double annualSal[6] = {43565.78, 75674.23,
90001.34,10923.45, 39845.82};
You also can define and initialize a character
array with individual characters:
• char grades[5] = {'A', 'B', 'C', 'D', 'F'};
Because a null zero is not in the last character
element, grades consists of individual
characters, but not a string.
Defining a string
The following name definition puts a string in
name:
• char italCity[7] = {'V', 'e', 'r', 'o', 'n', 'a', '\0'};
You have to admit that initializing such a
character array with a string is easier to do like
this:
• char italCity[7] = "Verona"; /* Automatic null
zero */
• int ages[5] = {5, 27, 65, 40, 92}; // Correct
• int ages[]; // Incorrect
• int ages[] = {5, 27, 65, 40, 92}; // Correct

• sizeof() returns the number of bytes you reserved


for the array, not the number of elements in which
you have stored a value. For example, if floating-
point values consume 4 bytes on your computer,
an 8-element floating-point array will take a total
of 32 bytes of memory, and 32 is the value
returned if you apply sizeof() to the array after you
define the array.
Input data into the array
Here we are iterating the array from 0 to 3 because the size
of the array is 4. Inside the loop we are displaying a message
to the user to enter the values. All the input values are
stored in the corresponding array elements using scanf
function.
void main( )
{
int x ;
int num[4] ; /* array declaration */
for (x=0; x<4;x++)
{ printf("Enter number %d \n", (x+1)); scanf("%d",
&num[x]); }}
Reading out data from an array

• Suppose, if we want to display the elements of


the array then we can use the for loop in C like
this.
for (x=0; x<4;x++)
{ printf("%d\n", num[x]); }
Example 1: Array Input/Output
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>
int main()
{ int values[5];
printf("Enter 5 integers: "); // taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{ scanf("%d", &values[i]); }
printf("Displaying integers: "); // printing elements of an array
for(int i = 0; i < 5; ++i)
{ printf("%d\n", values[i]); }
return 0; }
Output
Enter 5 integers: 1
-3
34
0
3
Displaying integers: 1
-3
34
0
3
Example 2: Calculate Average (First way)
// Consider a scenario where you need to find out the average marks obtained
by a class of 10 students in a test using arrays?
#include <stdio.h>
int main()
{ int marks[10], i, n, sum = 0, average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{ printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i]; }
average = sum/n;
printf("Average = %d", average);
return 0;
}
Second way:
void main( )
{ int avg, sum = 0 ;
int i ;
int marks[10] ; /* array declaration */
for ( i = 0 ; i <= 9 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 9 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 10 ;
printf ( "\nAverage marks = %d", avg ) ;
}
Third way:

#include <stdio.h>
int main() {
int marks[10], i, n, sum = 0;
float average;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i < n; ++i) {
printf("Enter number%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / n;
printf("Average = %.2f", average);
return 0;
}
Output

Enter n: 5
Enter number1: 45
Enter number2: 35
Enter number3: 38
Enter number4: 31
Enter number5: 49
Average = 39
Passing arrays between functions:

#include <stdio.h>
void display (int);
void main()
{
int i;
int marks[] = {55, 65, 75, 56, 78, 78, 90};
for (i = 0; i <= 6; i++)
display (marks[i]);
}
void display (int m)
{
printf (“%d ”, m);
}
O/P: 55 65 75 56 78 78 90

You might also like