So, I'm new in learning array structure, and I'm made a program that asks the user to determine first their array size, after the user inputs the data, I want to display all the data entered, here's the code:
{
int num, i;
printf("How many data you want to input? :");
scanf("%d", &num);
int datacapacity[num];
for(i=0; i<num; i++)
{
printf("Mark %d :", i+1);
scanf("%d", &datacapacity[i]);
}
printf("\nMark %d is %d", i+1, datacapacity[i]);
}
input
How many data you want to input? :4 // 4 entered from keyboard
Mark 1 :23 // 23 entered from keyboard
Mark 2 :23 // 23 entered from keyboard
Mark 3 :23 // 23 entered from keyboard
Mark 4 :12 // 12 entered from keyboard
Mark 5 is 4
The problem I have is it just printing Mark 5 is 4 How to print all the data the user entered?
#include<stdio.h>
int main() {
int num, i;
printf("How many data you want to input? :");
scanf("%d", &num);
int datacapacity[num];
for (i = 0; i < num; i++) {
printf("Mark %d :", i + 1);
scanf("%d", &datacapacity[i]);
}
for (i = 0; i < num; i++) {
printf("\nMark %d is %d", i + 1, datacapacity[i]);
}
}
Use the above code.
In the above line of the second for loop is used for printing the array elements it starts from index 0 .
Related
I have this program where it lets the user input an array of numbers.
For example, I would input an array size of 4 and input the numbers 40, 20, 1, and 8 as the elements in the array.
Input array size: 4
Input array elements: 40 20 1 8
The program also lets the user delete any elements from the array and the output should also show the element that was deleted.
For example, I would delete the number 40 from the elements. The output should be like this:
Enter the position of the element that you would like to delete: 1
The element 40 is completely deleted!
But instead of showing 40, the output goes like this:
Enter the position of the element that you would like to delete: 1
The element 20 is completely deleted!
Here is my source code:
int a[100];
int arraySize, elementPosition;
printf("\n Input Array Size: ");
scanf("%d", &arraySize);
printf("\n Input Array Elements: ");
for(i = 0; i < arraySize; i++)
{
scanf("%d, ", &a[i]);
}
printf("\n Enter the position of the element you would like to delete: ");
scanf("%d", &elementPosition);
int deletedElement = a[elementPosition];
if (elementPosition >= arraySize + 1)
{
printf("\n \n \t Invalid position! Enter numbers from 1 to %d only.\n", arraySize);
getch();
del();
}
else
{
for (i = elementPosition; i < arraySize - 1; i++)
{
a[i] = a[i+1];
}
printf("\n \The element %d is completely deleted!", deletedElement);
arraySize = arraySize - 1;
getch();
}
It would be a very big help if someone can point out where did I go wrong.
Array index starts from 0. This deletedElement = a[elementPosition]; should be changed to deletedElement = a[elementPosition - 1];.
Additionally, you need to make sure that subtracting 1 does not cause deletedElement to become less than 0.
Could you test it, the index begin from 0 the position from 1.
int a[100];
int arraySize, elementPosition;
printf("\n Input Array Size: ");
scanf("%d", &arraySize);
printf("\n Input Array Elements: ");
for(i = 0; i < arraySize; i++)
{
scanf("%d, ", &a[i]);
}
printf("\n Enter the position of the element you would like to delete: ");
scanf("%d", &elementPosition);
int deletedElement = a[elementPosition - 1 ];
if (elementPosition >= arraySize)
{
printf("\n \n \t Invalid position! Enter numbers from 1 to %d only.\n", arraySize);
getch();
del();
}
else
{
for (i = elementPosition - 1; i < arraySize - 2; i++)
{
a[i] = a[i+1];
}
printf("\n \The element %d is completely deleted!", deletedElement);
arraySize = arraySize - 1;
getch();
}
I am trying to scanf 5 numbers into an array in one command line prompt. I know how to store a value one by one into an array, but in this case I am trying to store 5 values in a line. I know how to hard code it when the limit is clear, but I want to code it based on a user inputted limit.
for example (hard code):
int arr[5];
scanf("%d %d %d %d %d", &arr[0], &arr[1], &arr[2], &arr[3], &arr[4]);
for (i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
would give me the values of arr[0] arr[1] arr[2] arr[3] arr[4].
BUT what if the size of aaa is defined by users, or defined by a macro that allows changes? How do you not hard code it?
#define MAX 10
int arr[MAX];
or
printf("what is the limit? : ");
scanf("%d", &limit);
int arr[limit];
I tried using a for loop but it doesn't work.
for(i=0;i<MAX; i++){
scanf("%d %d %d %d %d\n", &aaa[i]); //I want user to input 5 numbers in one line, but this format doesnt work.
}
To conclude/clarify my question.
I want user to input 5 numbers in one line. for example : 1 2 3 4 5 with a space in between. and I want it stored at arr[0] arr[1] arr[2] arr[3] arr[4]. Is there a way to not hard code this?
Help would be greatly appreciated! Thanks!
You want this:
#include <stdio.h>
int main()
{
int arr[5];
printf("Enter 5 numbers separated by space then press Enter\n");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 5; i++) {
printf(" %d", arr[i]);
}
}
Execution:
Enter 5 numbers separated by space then press Enter
1 2 3 4 5
1 2 3 4 5
You need to read inputs in the loop as below.
for(i=0;i<MAX; i++) {
scanf("%d", &aaa[i]);
}
This question already has answers here:
Accepting any number of inputs from scanf function
(6 answers)
Closed 5 years ago.
If I want to get input 3 numbers, I can write code like this:
scanf("%d %d %d", &a, &b, &c);
but how can I dynamically get the number of inputs from one line?
For example, if user enters N(number), then I have to get N integer number inputs from one line like above.
The input and output should be :
how many do you want to enter: 5
1 2 3 4 5
sum: 15
Since scanf returns the amount of variables filled you can loop until scanf has no more value to read or the count is matched:
int count = 0;
printf("how many do you want to enter: ");
scanf("%d", &count);
int val = 0;
int sum = 0;
int i = 0;
while(scanf("%d ", &val) == 1 && i++ < count)
sum += val;
As you don't know the size of inputs previously it's better to create a dynamic array based on the input size provided by the user. Input the size of the array and create an array of that size. Then you can easily loop through the array and do whatever you want with it.
int count = 0, sum = 0;
printf("how many do you want to enter: ");
scanf("%d", &count);
int *num = malloc(sizeof(int)*count);
for(int i = 0; i < count; i++) {
scanf("%d ", &num[i]);
//sum += num[i];
}
int main() {
int i, repeatName; // ints
char firstName[50]; // array to store users name
// get first name from user
printf("Please enter your first name: ");
scanf("%s", firstName);
// get amount of times user would like to repeat name
printf("How many times would you like to repeat your name?: ");
scanf("%i", &repeatName);
// tell user name has to be repeated at last one
if (repeatName < 1) {
printf("The name has to be repeated at least one (1) time. Try again: ");
scanf("%i", &repeatName);
}
// for loop to repeat name 'x' number of times
for (i = 0; i < repeatName; i++) {
printf("%s \n", firstName);
}
}
For example: If the user wanted to display their name 3 times it would say:
Your name
Your name
Your name
How can I get it to say:
Line 1 Your name
Line 2 Your name
Line 3 Your name
Use the i variable in the loop as the line number
for (i = 0; i < repeatName; ++i)
printf("Line %d %s\n", i + 1, firstName);
Be sure to add 1, because the loop index starts from 0. You want the first line to say "Line 1", not "Line 0", and so on.
Edit: When the line number is more than one digit, the output is not as pretty. To solve this, you can write
for (i = 0; i < repeatName; ++i)
printf("Line %-6d%s\n", i + 1, firstName);
This makes the line number take up at least 6 characters, and makes the number be left-justified:
Line 1 this is my string
Line 2 this is my string
Line 3 this is my string
Line 4 this is my string
Line 5 this is my string
Line 6 this is my string
Line 7 this is my string
Line 8 this is my string
Line 9 this is my string
Line 10 this is my string
There is no way to do that automatically, as far as I know. But you can make your i variable in the for loop act as a line counter too, since for each iteration you print a line:
// for loop to repeat name 'x' number of times
for (i = 0; i < repeatName; i++) {
printf("Line %d %s \n", i + 1 /* Lines are not 0 based */ , firstName);
}
Just add the loop index when you're printing:
for (i = 0; i < repeatName; i++) {
printf("Line %d %s \n", i+1, firstName);
}
Try This code
#include <stdio.h>
int main()
{
int i, repeatName; // ints
char firstName[50]; // array to store users name
// get first name from user
printf("Please enter your first name: ");
scanf("%s", firstName);
// get amount of times user would like to repeat name
printf("How many times would you like to repeat your name?: ");
scanf("%i", &repeatName);
// tell user name has to be repeated at last one
if (repeatName < 1) {
printf("The name has to be repeated at least one (1) time. Try again: ");
scanf("%i", &repeatName);
}
// for loop to repeat name 'x' number of times
for (i = 1; i <= repeatName; i++) {
printf("Line %d Your name %s \n",i,firstName);
}
}
I know that I can use
scanf("%d %d %d",&a,&b,&c):
But what if the user first determines how many input there'd be in the line?
You are reading the number of inputs and then repeatedly (in a loop) read each input, eg:
#include <stdio.h>
#include <stdlib.h>
int main(int ac, char **av)
{
int numInputs;
int *input;
printf("Total number of inputs: ");
scanf("%d", &numInputs);
input = malloc(numInputs * sizeof(int));
for (int i=0; i < numInputs; i++)
{
printf("Input #%d: ", i+1);
scanf("%d", &input[i]);
}
// Do Stuff, for example print them:
for (int i=0; i < numInputs; i++)
{
printf("Input #%d = %d\n", i+1, input[i]);
}
free(input);
}
Read in the whole line, then use a loop to parse out what you need.
To get you started:
1) Here is the manual page for getline(3):
http://man7.org/linux/man-pages/man3/getline.3.html
2) Some alternatves to getline:
How to read a line from the console in C?
3) Consider compressing spaces:
How do I replace multiple spaces with a single space?
4) Use a loop for parsing. You might consider tokenizing:
Tokenizing strings in C
5) Be careful and remember that your user could enter anything.
#include <conio.h>
#include <stdio.h>
main()
{
int a[100],i,n_input,inputs;
printf("Enter the number of inputs");
scanf("%d",&n_input);
for(i=0;i<n_input;i++)
{
printf("Input #%d: ",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n_input;i++)
{
printf("\nInput #%d: %d ",i+1,a[i]);
}
}
/*
_______________This program is in C Programming Language_______________
We have to directly enter all the elements in one line giving spaces between them. Compiler will automatically ends the for loop I have used and assign the value to their respective variables or array indexes. Below program and output will give you better understanding.
*/
#include <stdio.h>
int main()
{
//taking no of inputs from user
int len;
printf("Enter the number of inputs you want to enter : ");
scanf("%d", &len);
int i;
//defined an array for storing multiple outputs
int arr[100];
//included a printf statement for better understanding of end user
printf("Enter the inputs here by giving space after each input : ");
/*here is the important lines of codess for taking multiple inputs on one line*/
for (i=0;i<len;i++)
{
scanf("%d", &arr[i]);
}
printf("Your entered elements is : ");
for (i=0;i<len;i++)
{
printf("%d ", arr[i]);
}
}
/*
OUTPUT :
Enter the number of inputs you want to enter : 5
5 5 5 8 7
Your entered elements is : 5 5 5 8 7
*/