Home > Backend Development > C++ > body text

C program to input an array of sequences of integers separated by spaces

PHPz
Release: 2023-08-25 11:33:08
forward
3328 people have browsed it

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
Copy after login

Output

‘Array elements are -’ 1, 2, 3, 4, 5
Copy after login
The Chinese translation of

Explanation

is:

Explanation

The input contains 5 space-separated integers.

enter

99 76 87 54 23 56 878 967 34 34 23
Copy after login

Output

‘Array elements are -’ 99, 76, 87, 54, 23, 56, 878, 967, 34, 34, 23
Copy after login
The Chinese translation of

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;
}
Copy after login

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,
Copy after login
  • 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;
}
Copy after login

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
Copy after login
  • 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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template