Arrays in C
- Sol Lee
- May 25, 2024
- 1 min read
Let’s say we have a random list of numbers: 2, 10, 35, 100
In an array, we don’t call “2” the 1st number, “10” the 2nd number, “35” the third number, and more.
In an array, we call “2” the 0 number, “10” the 1st number, “35” the 2nd number, and ongoing.
Array Declaration:
2, 10, 35, 100 is declared as Arr[4]
Declaring Integer Array Code Example :
#include <stdio.h>
int main()
{
int arr[5];
return 0;
}
Declaring Character Array Code Example :
#include <stdio.h>
int main()
{
char arr[5];
return 0;
}
When we want to declare an array of numbers, we can:
#include <stdio.h>
int main()
{
int arr[5] = {1, 2, 3, 4, 5};
return 0;
}
When we want to use ARRAY + LOOP:
#include <stdio.h>
int main()
{
float arr[5];
for (int x = 0; x < 5; x++)
{
arr[x] = (float)x * 3.3;
}
return 0;
}
When we want to call array:
#include <stdio.h>
int main()
{
int arr[4] = { 2, 10, 35, 100};
printf("The number at arr[2]: %d \n", arr[2]);
printf("The number at arr[1]: %d \n", arr[1]);
printf("The number at arr[0]: %d", arr[0]);
Return 0;
}
*Reminder that “\n” is skipping a line
Output:
The number at arr[2]: 35
The number at arr[1]: 10
The number at arr[0]: 2
APPLICATION TIME!
Go to replit(make a free account), choose c, and start coding
Today’s challenge:
Write an array and calculate the average of all the numbers within the array!
Comments