Welcome to the fascinating world of C Programming! In this lesson, we'll delve into one of the fundamental data structures: Arrays. By the end of this tutorial, you'll be comfortable with array declaration and usage, ready to tackle more complex programming tasks. Let's get started! š
Arrays are a collection of variables of the same data type, indexed by a number (or an expression). They allow us to store and manipulate multiple values efficiently, making them essential in many real-world applications. š”
To declare an array in C, we specify the data type, array name, and size enclosed within square brackets []. Let's see an example:
int numbers[5]; // Declares an array named 'numbers' with 5 elementsIn this example, we've declared an array called numbers that can hold 5 integer values.
To initialize an array, we can assign values directly when declaring it. Here's an example:
int numbers[5] = {1, 2, 3, 4, 5}; // Initializes an array named 'numbers' with given valuesYou can also initialize arrays with fewer values, and the remaining elements will be automatically set to zero. For example:
int numbers[7] = {1, 2, 3}; // The remaining elements will be initialized to zeroš” Pro Tip: Initializing arrays when declaring them can save time and help avoid errors.
To access array elements, we use an index. The index is a number within square brackets [], starting from 0. Here's an example:
#include <stdio.h>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printf("The first element is: %d\n", numbers[0]); // Output: The first element is: 1
printf("The last element is: %d\n", numbers[4]); // Output: The last element is: 5
return 0;
}In this example, we've accessed the first and last elements of the numbers array.
C supports various types of arrays, including:
We'll explore multidimensional arrays in future lessons, but for now, let's dive into a couple of examples to solidify your understanding of array declaration. š”
In this example, we'll create an array, input values, and then find the sum of even numbers.
#include <stdio.h>
int main() {
int numbers[5], sum = 0;
printf("Enter 5 numbers:\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &numbers[i]);
}
printf("Even numbers are:\n");
for (int i = 0; i < 5; i++) {
if (numbers[i] % 2 == 0) {
sum += numbers[i];
printf("%d ", numbers[i]);
}
}
printf("\nThe sum of even numbers is: %d\n", sum);
return 0;
}In this example, we've created an array numbers, input values from the user, and then found the sum of even numbers.
In this example, we'll create a simple address book containing names and phone numbers.
#include <stdio.h>
#include <string.h>
#define MAX_CONTACTS 5
struct Contact {
char name[50];
char phone[20];
};
int main() {
struct Contact contacts[MAX_CONTACTS];
int numContacts = 0;
printf("Simple Address Book\n");
while (numContacts < MAX_CONTACTS) {
printf("\nEnter contact %d details:\n", numContacts + 1);
printf("Name: ");
scanf("%[^\n]s", contacts[numContacts].name);
printf("Phone: ");
scanf("%s", contacts[numContacts].phone);
numContacts++;
printf("Do you want to add another contact? (y/n)\n");
char choice[2];
scanf("%s", choice);
if (strcmp(choice, "y") == 0 || strcmp(choice, "Y") == 0) {
continue;
}
}
printf("\nContacts:\n");
for (int i = 0; i < numContacts; i++) {
printf("\nContact %d\n", i + 1);
printf("Name: %s\n", contacts[i].name);
printf("Phone: %s\n", contacts[i].phone);
}
return 0;
}In this example, we've created a simple address book with a maximum of 5 contacts. Users can input names and phone numbers, and the program will display the contacts upon completion.
How do you declare an array of 10 integers in C?
That's it for this lesson on C array declaration! In the next lesson, we'll explore array manipulation and other useful functions to help you become a proficient C programmer. š”
Keep learning, and happy coding! š