


C program to input an array of sequences of integers separated by spaces
Problem Statement
Write a C program that takes space-separated integers as array input.
Sample Examples
enter
1 2 3 4 5
Output
‘Array elements are -’ 1, 2, 3, 4, 5
Explanation
is:Explanation
The input contains 5 space-separated integers.
enter
99 76 87 54 23 56 878 967 34 34 23
Output
‘Array elements are -’ 99, 76, 87, 54, 23, 56, 878, 967, 34, 34, 23
Explanation
is:Explanation
The input contains 11 space-separated integers.
method one
In this method, we will store the space-separated integers from the input in a single-dimensional array.
algorithm
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.
Example
#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; }
Output
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).
Method 2 (Input the array into a two-dimensional array)
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 .
algorithm
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.
Example
#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; }
Output
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)
in conclusion
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Recently, many Win11 users have encountered the problem that the input experience dialog box always flickers and cannot be turned off. This is actually caused by the default system services and components of Win11. We need to disable the relevant services first, and then disable the input experience service. Solved, let’s try it out together. How to turn off the input experience in win11: First step, right-click the start menu and open "Task Manager". Second step, find the three processes "CTF Loader", "MicrosoftIME" and "Service Host: Textinput Management Service" in order, right-click "End Task" "The third step, open the start menu, search and open "Services" at the top. The fourth step, find "Textinp" in it
![Windows input encounters hang or high memory usage [Fix]](https://img.php.cn/upload/article/000/887/227/170835409686241.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
The Windows input experience is a key system service responsible for processing user input from various human interface devices. It starts automatically at system startup and runs in the background. However, sometimes this service may automatically hang or occupy too much memory, resulting in reduced system performance. Therefore, it is crucial to monitor and manage this process in a timely manner to ensure system efficiency and stability. In this article, we will share how to fix issues where the Windows input experience hangs or causes high memory usage. The Windows Input Experience Service does not have a user interface, but it is closely related to handling basic system tasks and functions related to input devices. Its role is to help the Windows system understand every input entered by the user.

Given below is a C language algorithm to convert Roman numerals to decimal numbers: Algorithm Step 1 - Start Step 2 - Read Roman numerals at runtime Step 3 - Length: = strlen(roman) Step 4 - For i=0 to Length-1 Step 4.1-switch(roman[i]) Step 4.1.1-case'm': &nbs

Linked lists use dynamic memory allocation, i.e. they grow and shrink accordingly. They are defined as collections of nodes. Here, a node has two parts, data and links. The representation of data, links and linked lists is as follows - Types of linked lists There are four types of linked lists, as follows: - Single linked list/Singly linked list Double/Doubly linked list Circular single linked list Circular double linked list We use the recursive method to find the length of the linked list The logic is -intlength(node *temp){ if(temp==NULL) returnl; else{&n

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define

Using strings or characters is sometimes very useful when solving some logic programming problems. A string is a collection of characters, which is a 1-byte data type used to hold symbols in ASCII values. Symbols can be English letters, numbers, or special characters. In this article, we will learn how to check if a character is an English letter or a letter of the alphabet using C++. Checking the isalpha() function To check if a number is a letter, we can use the isalpha() function in the ctype.h header file. This takes a character as input and returns true if it is an alphabet, false otherwise. Let us look at the following C++ implementation to understand the usage of this function. The Chinese translation of Example is: show
