Home > Article > Backend Development > C program to input an array of sequences of integers separated by spaces
Write a C program that takes space-separated integers as array input.
1 2 3 4 5
‘Array elements are -’ 1, 2, 3, 4, 5The Chinese translation of
The input contains 5 space-separated integers.
99 76 87 54 23 56 878 967 34 34 23
‘Array elements are -’ 99, 76, 87, 54, 23, 56, 878, 967, 34, 34, 23The Chinese translation of
The input contains 11 space-separated integers.
In this method, we will store the space-separated integers from the input in a single-dimensional array.
Step 1 − Create an array of specific length. Here, we have created an array of length 100.
Step 2 - In the input box, we ask the user to enter elements separated by spaces.
Step 3 - We use the scanf() function to accept integer input and store it at the "current index" index of the array.
Step 4 - We continue to accept input until the user presses the Enter key or enters a total of 100 elements.
Step 5 - Loop through the array and print all elements.
#include <stdio.h> int main(){ int currentIndex = 0; // Initialize an array int arr[100]; printf("Enter maximum 100 numbers and stop\n"); // Take input, and stop the loop if the user enters a new line or reaches 100 elements do{ // store an array index scanf("%d", &arr[currentIndex++]); } while (getchar() != '\n' && currentIndex < 100); // change the size of the array equal to the number of elements entered. arr[currentIndex]; // Print the array elements printf("Array elements are: "); for (int i = 0; i < currentIndex; i++) { printf("%d, ", arr[i]); } return 0; }
Enter maximum 100 numbers and stop 1 2 3 4 5 6 7 8 Array elements are: 1, 2, 3, 4, 5, 6, 7, 8,
Time complexity - The time complexity of taking N elements from the input is O(N).
Space Complexity - The space complexity of storing N elements in an array is O(N).
In this approach, we will take space separated integer values as input and store them in a 2D array. We can take space separated integers as input as we did in the first approach and manage array indexes to store elements in a 2D array .
Step 1 − Create a 2D array.
Step 2 - Use two nested loops to manage the indexing of the 2D array.
Step 3 - Ask the user to enter array elements separated by spaces.
Step 4 − Get the element from the input and store it at a specific index position in the 2D array.
Step 5 - Print a 2D array using two nested loops.
#include <stdio.h> int main(){ int currentIndex = 0; // taking input from 2d array int array[3][3]; printf("Enter 9 values for 3x3 array : \n"); for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { scanf("%d", &array[i][j]); } } printf("Array values are : \n"); // printing 2d array for (int i = 0; i < 3; i++) { printf("\n"); for (int j = 0; j < 3; j++) { printf("%d ", array[i][j]); } } return 0; }
Enter 9 values for 3x3 array : 1 2 3 4 5 6 7 8 9 Array values are : 1 2 3 4 5 6 7 8 9
Time complexity - O(N*M), where N is the total number of rows and M is the total number of columns.
Space complexity − O(N*M)
We learned to take space-separated integers as input and store them in an array. Additionally, we learned to store input elements separated by spaces in a multidimensional array. The user can take any type of space-separated elements from user input as an array.
The above is the detailed content of C program to input an array of sequences of integers separated by spaces. For more information, please follow other related articles on the PHP Chinese website!