Array is a collection of variables of the same type stored at contiguous memory locations.
It is used to store a collection of data, but it is often more useful to think of an array as a collection of variables. Instead of declaring individual variables, such as number0, number1, ..., number99, you can declare one array variable such as numbers and use numbers[0], numbers[1], ..., numbers[99] to access the individual variables.
Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. An array can hold many values under a single name, and you can access the values by referring to the index number.
Index number is the position of the element in the array.
It starts from 0 to the last being (size of array - 1)
For instance if the size of an array is 10, the indexes will be as follows
| Element Number | Index Number |
| 1 | 0 |
| 2 | 1 |
| 3 | 2 |
| 4 | 3 |
| 5 | 4 |
| 6 | 5 |
| 7 | 6 |
| 8 | 7 |
| 9 | 8 |
| 10 | 9 |
As you can see, the last index number is one less than the size of the array because the index number starts from 0.
You can picture an array as a bookshelf where books are stored.
The books represents the data/elements to be stored and must have the same data type as it is inappropriate to place other items which are not books in a bookshelf.
For easy location of the books, the shelf is numbered so you know which book is at what shelf number.
The shelf number (slot) represents the index number. In this case the shelf numbering starts from 0

Array in C programming can be declared in any of the following ways:
data_type array_name[size];data_type array_name[] = {value1, value2, ..., valueN};Where data_type is the type of data to be stored in the array, array_name is the name of the array, and size is the number of elements that can be stored in the array.
When you declare an array, you must specify the type of data it will hold and the number of elements it can store. The size of the array must be a positive integer constant.
For example, to declare an array of integers that can hold 5 elements, you can use the following syntax:
int numbers[5];
This declaration creates an array named numbers that can hold 5 integer values.
You only don't specify the size when initializing the array with values:
int numbers[] = {1, 2, 3, 4, 5};
In the above declaration, the compiler automatically determines the size of the array based on the number of elements provided. The array will therefore have a size of 5 since there are 5 elements in the initialization list.
Once an array is declared, you can assign values to its elements using the index number:
numbers[index] = value;
Where index is the position of the element in the array and value is the value to be assigned.
For example to assign values to the first n elements, you can write the code below:
numbers[0] = 10;
numbers[1] = 5;
numbers[2] = 8;
numbers[3] = 3;
numbers[4] = 7; and so on until numbers[n-1] = value;
Just like the bookshelf analogy, when you want a particular book, you have to go to the shelf where it is located and pick the book from that slot, you also access array elements using their index.
You can access the elements of an array using the index number:
numbers[index];
index is the position of the element in the array.
For example, to access the elements (n) of an array , you can use the following code:
numbers[0];
numbers[1];
numbers[2];
numbers[3];
numbers[4]; and so on until numbers[n-1];
Note: You will get a compilation error if you try to access an element outside the bounds of the array (index is negative or greater than or equal to the array size).
This also applies to assigning values to array elements. The index must be within the valid range of the array.
You can as well use a loop to access all elements of an array if you know the size of the array.
The initial control variable for the loop is usually set to 0, and the loop continues until it reaches the size of the array.
For example, if the size of the array is n and the control variable is i, you can access all elements:
for (int i = 0; i < n; i++) {
variable = numbers[i];
}
You can also use a loop to print all elements of an array:
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
This prints each element of the array in horizontal order.
If you wish to print the elements in vertical order, you can add a newline character:
for (int i = 0; i < n; i++) {
printf("%d\n", numbers[i]);
}
The size of an array is the number of elements it can hold. You can find the size of an array using the sizeof operator:
int size = sizeof(numbers) / sizeof(numbers[0]);
Where numbers is the array
This calculates the total size of the array in bytes and divides it by the size of a single element to get the number of elements.
Note: in C programming, each element in the array is given the same number of bytes.
Since for certainty, the array has index 0, the size of the element of index 0 is used to divide the total size to know how many elements are in the array.
But if you already know the size of the array, you can simply use that value instead of calculating it.
You can change the value of an array element by assigning a new value to it:
numbers[index] = new_value;
Where index is the position of the element in the array and new_value is the value to be assigned.
For example, to change the value of the element at index 2 to 15, you can use the following code:
numbers[2] = 15;
Likewise to change the first element, you can use the following code:
numbers[0] = 10;
And to change the last element, you can use the following code:
numbers[n-1] = 20;
Where n is the size of the array.
Let's have fun by developing a lottery game!
Our lottery game will ask the user to enter how many lotto numbers they want to stake:
The game will keep the user's numbers in an array.
It will also generate random lotto numbers based on the user's number of stakes.
It will then compare the user's numbers with the generated numbers to determine how many they match.
The game will display the winning numbers and how much the user has won if each winning number is awarded $100.
You have all the necessary knowledge from the previous lessons to build this game except how to generate the random numbers in C.
To generate random numbers in C, you can use the rand() function from the stdlib.h library. This function returns a random integer between 0 and RAND_MAX.
To generate a random number within a specific range, you can use the following formula:
random_number = (rand() % range) + min;
Where range is the size of the range of numbers you want to generate, and min is the minimum number in the range.
For example, to generate a random number between 1 and 50, you can use the following code:
int random_number = (rand() % 50) + 1;
This will generate a random number between 1 and 50.
If the numbers that can be staked ranges between 1 and 100, the random number generation will be:
int random_number = (rand() % 100) + 1;
To ensure that you get different random numbers each time you run the program, you can seed the random number generator using the srand() function with the current time as the seed:
srand(time(0));
This will seed the random number generator with the current time, ensuring that you get different random numbers each time you run the program.
Our lotto game code will be:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int n;
printf("Enter the number of lotto numbers you want to stake: ");
scanf("%d", &n);
int stakes[n];//Declare the array of size n
//Take each number
for (int i = 0; i < n; i++) {
do{
printf("Enter your lotto number %d: ",i+1);
scanf("%d", &stakes[i]);
}while((stakes[i] < 1)||(stakes[i] > 100));
}
//Generate random numbers
srand(time(0));
int winning_numbers[n];
for (int i = 0; i < n; i++) {
//Store the generated random numbers
winning_numbers[i] = (rand() % 100) + 1;
}
//Loop through the user's stakes to find how many matches the winning numbers
int matches = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (stakes[i] == winning_numbers[j]) {
matches++;
}
}
}
printf("The winning numbers are:\n");
for(int i = 0; i < n;i++){
printf("%d\n",winning_numbers[i]);
}
double prize_per_winning = 100.0;
double winnings = matches * prize_per_winning;
printf("Price per winning is $%.2f\n",prize_per_winning);
printf("You matched %d number(s) and won $%.2f!\n", matches, winnings);
return 0;
}
In the above code, there is a nested loop used to compare the user's stakes with the winning numbers.
The outer loop iterates through each user stake, and the inner loop compares it with each winning number.
The srand(time(0)); function is used to seed the random number generator with the current time, ensuring that different random numbers are generated each time the program is run.
We can put our lotto game in a loop so user can play multiple times and enter 0 to exit the game.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int exit = 1;
while(exit != 0){
int n;
printf("Enter the number of lotto numbers you want to stake: ");
scanf("%d", &n);
int stakes[n];//Declare the array of size n
//Take each number
for (int i = 0; i < n; i++) {
do{
printf("Enter your lotto number %d: ",i+1);
scanf("%d", &stakes[i]);
}while((stakes[i] < 1)||(stakes[i] > 100));
}
//Generate random numbers
srand(time(0));
int winning_numbers[n];
for (int i = 0; i < n; i++) {
//Store the generated random numbers
winning_numbers[i] = (rand() % 100) + 1;
}
//Loop through the user's stakes to find how many matches the winning numbers
int matches = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (stakes[i] == winning_numbers[j]) {
matches++;
}
}
}
printf("The winning numbers are:\n");
for(int i = 0; i < n;i++){
printf("%d\n",winning_numbers[i]);
}
double prize_per_winning = 100.0;
double winnings = matches * prize_per_winning;
printf("Price per winning is $%.2f\n",prize_per_winning);
printf("You matched %d number(s) and won $%.2f!\n", matches, winnings);
printf("Enter 0 to exit or any other number to play again: ");
scanf("%d", &exit);
}
return 0;
}