How Can I Fix My C Program to Handle Multiple Inputs for a Linear Equation?

  • Thread starter Thread starter ramses07
  • Start date Start date
  • Tags Tags
    Multiple
AI Thread Summary
The discussion focuses on fixing a C program designed to handle multiple inputs for a linear equation, specifically y = mx + b. The original code contains a misplaced semicolon in the loop, preventing it from executing correctly and only allowing a single value to be processed. Suggestions include removing the semicolon, properly structuring the loop, and ensuring calculations for y are performed within the loop. Additionally, there are concerns about resetting the loop control variable and the output only displaying two y values. Proper indentation and code structure are emphasized for better readability and functionality.
ramses07
Messages
11
Reaction score
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
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.
 
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:
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]);
	
}
 
Do you understand the extent of your loop? Why did you not follow DH's recommendation on your loop extent?
 
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]);
	
}
 

Similar threads

Replies
3
Views
1K
Replies
3
Views
1K
Replies
1
Views
10K
Replies
12
Views
2K
Replies
12
Views
2K
Replies
4
Views
1K
Replies
2
Views
2K
Back
Top