C Program to store n numbers in array
As the programming world keeps on evolving, we know that it can get very difficult and problematic at some point but if we understand the basics well then there won’t be any issues. C programming can prove to be very efficient when it comes to a person who is just beginning to learn the programming world, it will help the person understand the basics and form a grip over it. One key skill set for every budding developer is learning how to handle data and arrays are great tool for that. Using array, we can efficiently store our data and the access mechanism is faster in array as compared to other forms of data types. The main goal of this tutorial is not just how enter numbers but to carefully guide you towards how we can store data in an array and make efficient use of the same. By learning this you’ll not only be able to handle arrays but also, you’ll be able to understand more advanced key concepts such as data structures and algorithms.
Basic Concepts
An array is generally like a container that stores similar types of data together. In the programming world, we just need to inform the computer system about the type of data we want to store in the array and how many of them we will be storing. For example, if we want to store n integers in an array, the syntax will be like this: -
int numb[n];
In the above example, numb stands for the numbers we are storing in an array and ‘n’ will be the size of the array. The index of the array will begin from 0 and goes up till n-1.
Storing n numbers in the array
In this section, we are going to see how we can store n numbers in an array.
#include <stdio.h>
int main() {
int n;
// Get the number of elements from the user
printf("Hello! Please enter the numbers for your array: ");
scanf("%d", &n);
// Declare an array of size n
int numbers[n];
// Input N numbers from the user
printf("kindly enter %d digits:\n", n);
for (int i = 0; i < n; i++) {
printf("Number %d: ", i + 1);
scanf("%d", &numbers[i]);
}
// Display the entered numbers
printf("\nThe final array is:\n");
for (int i = 0; i < n; i++) {
printf("%d ", numbers[i]);
}
return 0;
}
Output:
Example 2:
#include <stdio.h>
// Writing a function to swap the two integers
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
// Writing a function that will perform bubble sort on the array.
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
int main() {
// Declare an array with a capacity of at least 200 elements
int numbers[200];
// Variable to store the number of elements user wants to input
int n;
printf("Hello! Please enter any number ranging less than 200: ");
scanf("%d", &n);
if (n <= 0 || n > 200) {
printf("Hello! Please enter any number 1 and 200.\n");
return 1;
}
printf("Enter %d numbers:\n", n);
for (int i = 0; i < n; ++i) {
printf("Digit %d: ", i + 1);
scanf("%d", &numbers[i]);
}
printf("\nThe original array is:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", numbers[i]);
}
bubbleSort(numbers, n);
printf("\nThe Sorted array is:\n");
for (int i = 0; i < n; ++i) {
printf("%d ", numbers[i]);
}
int sum = 0;
for (int i = 0; i < n; ++i) {
sum += numbers[i];
}
float average = (float)sum / n;
printf("\nAverage of the numbers: %.2f\n", average);
return 0;
}
Output:
Conclusion
This tutorial shows us how we can easily declare arrays, take input from the users and easily build our key concepts for the C programming. We can build this type of data by sorting them or performing other actions on the data we have. Learning these basic key concepts is crucial when it comes to the programming world scenario.