Multiple inputs c-programming

  • Thread starter ramses07
  • Start date
  • Tags
    Multiple
In summary: This is better indentation. You should also use spaces between your keywords and the code they modify. Additionally, you need to indent the control statements (loops, if blocks, and so on).In summary, the code fails to return any values from the equation when entered with multiple inputs. The loop control variable x is reset to whatever value is entered in the for loop.
  • #1
ramses07
11
0

Homework Statement


I'm trying to create a c program that will allow for multiple inputs for a simple linear equation, and will print those values. the program I've created only returns one value

Homework Equations



y = mx + b

The Attempt at a Solution


Code:
#include <stdio.h>
#include <math.h>

int main () 
{

	int numbers[2];
	int x,y;
		printf("Enter x data set:");
	
		for(x=0;x<2;x++);

		x = numbers[2];
		scanf("%d",&numbers[2]); 

		 
	y =   5 * numbers[2] + 4;
	printf("the y values are; %d", y);
	
}
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
ramses07, notice how I edited your opening post. Your code looks like code because I added [ code ] and [ /code ] tags (delete the spaces after/before the opening/closing square braces, or simpler, use the "code" icon).

Your problem is your loop for(x=0;x<2;x++);. You need to
  • Get rid of that semicolon!
    That semicolon makes this loop a no-op, code that does absolutely nothing.
  • Replace that semicolon with an open brace.
    This means you need a close brace somewhere later, after the body of your loop.
  • Make the lines that follow form the body of your loop.
 
  • #3
You have declared y as an integer and you have a single print statement. Since you have specifically told it to print one value, I don't understand why you are surprised that it only prints one value.

EDIT: I see DH beat me to it. Your problem is, as he pointed out, your loop control
 
Last edited:
  • #4
so this is what I've come up with, but I'm only getting zeros

Code:
#include <stdio.h>
#include <math.h>

int main () 
{

	int numbers[20], y[20];
	int x;
		printf("Enter x data set:");
	
		for(x=0;x<20;x++)
		{scanf("%d",&numbers[x]); 
		
		x = numbers[x];}

		 
	y[x] =   5 * numbers[x] + 4;

	printf("the y values are; \n%d\n%d\n", y[1], y[0]);
	
}
 
  • #5
Do you understand the extent of your loop? Why did you not follow DH's recommendation on your loop extent?
 
  • #6
ramses07 said:
so this is what I've come up with, but I'm only getting zeros

Code:
#include <stdio.h>
#include <math.h>

int main () 
{

	int numbers[20], y[20];
	int x;
		printf("Enter x data set:");
	
		for(x=0;x<20;x++)
		{scanf("%d",&numbers[x]); 
		
		x = numbers[x];}

		 
	y[x] =   5 * numbers[x] + 4;

	printf("the y values are; \n%d\n%d\n", y[1], y[0]);
	
}
1. Why do you have this statement? x = numbers[x];
x is your loop control variable, so you should not be resetting it in the body of your loop.
2. The statement y[x] = 5 * numbers[x] + 4; is outside the loop, so it executes once. Furthermore, since the last iteration of the loop reset x to whatever value you entered, the statement where you set y[x] will attempt to set the element of the array whose index is x. This element might or not be within the array.
3. Your printf statement prints only two values of the y array.

Your indentation could be improved. Two important principles are that 1) statements that execute the same number of times should be at the same indentation level, and 2) the bodies of control statements (loops, if blocks, and so on) should be indented.
ed the same. Using good indentation helps readers of your code understand its structure more easily. This is how I would indent your code.

Code:
#include <stdio.h>
#include <math.h>

int main () 
{

   int numbers[20], y[20];
   int x;

   printf("Enter x data set:");  // printf statement not indented

   for(x=0;x<20;x++)
   {                                           // Braces for loop body are clearly evident
      scanf("%d",&numbers[x]); 	// Body of for loop indented
      x = numbers[x];
   }
		 
   y[x] =   5 * numbers[x] + 4;
   printf("the y values are; \n%d\n%d\n", y[1], y[0]);
	
}
 

1. What is multiple inputs c-programming?

Multiple inputs c-programming refers to the process of taking in multiple values or inputs from the user in a C program. This allows for more dynamic and versatile programs that can handle various inputs and perform different actions based on those inputs.

2. How do I take in multiple inputs in my C program?

To take in multiple inputs in your C program, you can use the scanf() function. This function allows you to specify the data type of each input and store the input values in variables for later use in your program.

3. Can I use a loop to take in multiple inputs in my C program?

Yes, you can use a loop, such as a for or while loop, to take in multiple inputs in your C program. This is useful when you need to take in a large number of inputs or when the number of inputs is unknown.

4. How can I handle errors when taking in multiple inputs in my C program?

You can handle errors in multiple inputs c-programming by checking the return value of the scanf() function. If the function returns a value less than the number of inputs specified, it means an error has occurred and you can handle it accordingly in your code.

5. Are there any best practices for using multiple inputs in a C program?

Yes, it is recommended to always validate user inputs and handle errors appropriately when using multiple inputs in a C program. It is also good practice to use clear and descriptive variable names to make your code more readable and maintainable.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
669
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
883
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
927
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top