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
Click For Summary

Discussion Overview

The discussion revolves around a C programming problem where the user seeks assistance in modifying their code to handle multiple inputs for a linear equation, specifically the equation y = mx + b. The focus is on debugging the code to ensure it correctly processes multiple inputs and outputs corresponding y values.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant points out that the loop control statement contains a semicolon, which causes the loop to do nothing, suggesting the removal of the semicolon to allow the loop to function correctly.
  • Another participant notes that the variable y is declared as an integer and questions why only one value is printed, implying that the print statement is not set up to handle multiple outputs.
  • A participant expresses confusion about the extent of the loop and questions why the user did not follow previous recommendations regarding the loop structure.
  • Concerns are raised about resetting the loop control variable x within the loop body, which could lead to unintended behavior.
  • It is noted that the assignment of y[x] occurs outside the loop, which means it only executes once, potentially leading to incorrect indexing of the y array.
  • Suggestions are made to improve code indentation for better readability and understanding of the code structure.

Areas of Agreement / Disagreement

Participants generally agree on the issues present in the user's code, but there is no consensus on the best approach to resolve the problems, as multiple suggestions and corrections are offered without a clear resolution.

Contextual Notes

Limitations include potential misunderstandings about loop control variables, array indexing, and the scope of variable assignments. The discussion does not resolve these issues definitively.

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 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
9
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K