Solving a Matrix with Elimination: Inputting the Right Number of Values

  • Thread starter Thread starter darwinharianto
  • Start date Start date
  • Tags Tags
    Elimination Matrix
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to inputting values into a matrix using the C programming language. Participants explore the use of the scanf function for reading input, the structure of the code, and how to effectively manage input for a matrix of varying sizes. The focus is primarily on debugging and improving the input process within the context of a homework assignment.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a problem with needing to input four values for the first row of a matrix despite only wanting to input three, suggesting a need for code modification.
  • Another participant questions the necessity of using scanf and suggests checking its return value for input validation.
  • Multiple participants propose removing the newline character from scanf calls to avoid input issues, indicating that scanf should skip whitespace.
  • There is a suggestion to use loops for inputting matrix values to reduce redundancy in the code, with one participant providing a sample loop structure.
  • Participants discuss the importance of correctly indexing the dimensions of the matrix to avoid off-by-one errors.
  • One participant expresses difficulty in displaying the results of the matrix calculations and seeks clarification on how to format the output correctly.
  • Another participant provides a code snippet to help format the output of the results as X1, X2, etc., but there is some confusion regarding the implementation.
  • There is a brief exchange about the participant's experience level with programming and their reasons for using C.

Areas of Agreement / Disagreement

Participants generally agree on the need to modify the scanf calls and to use loops for input, but there is no consensus on the best way to display the results or on the necessity of certain coding practices. The discussion remains unresolved regarding the optimal output formatting.

Contextual Notes

Some participants mention the potential for input errors and the importance of validating input, but specific assumptions about the matrix size and structure are not fully explored. The discussion also highlights limitations in the original code without providing a complete solution.

Who May Find This Useful

This discussion may be useful for students learning C programming, particularly those working on matrix operations and input handling in coding assignments.

darwinharianto
Messages
42
Reaction score
0

Homework Statement


Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
    int i,j,k,n;
    float A[3][4];
    float x[3],c,sum=0;
    n=2;
    printf("first row\n");
    scanf("%f",&A[0][0]);
    scanf("%f",&A[0][1]);
    scanf("%f\n",&A[0][2]);
    printf("second row\n");
    scanf("%f",&A[1][0]);
    scanf("%f",&A[1][1]);
    scanf("%f\n",&A[1][2]);
    printf("third row\n");
    scanf("%f",&A[2][0]);
    scanf("%f",&A[2][1]);
    scanf("%f\n",&A[2][2]);
    printf("designated value\n");
    scanf("%f",&A[0][3]);
    scanf("%f",&A[1][3]);
    scanf("%f\n",&A[2][3]);
    for(j=0; j<=n; j++)
    {
        for(i=0; i<=n; i++)
        {
            if(i>j)
            {
                c=A[i][j]/A[j][j];
                for(k=0; k<=n+1; k++)
                {
                    A[i][k]=A[i][k]-c*A[j][k];
                }
            }
        }
    }
    x[n]=A[n][n+1]/A[n][n];
    for(i=n-1; i>=0; i--)
        {
               sum=0;
               for(j=i+1; j<=n; j++)
                 {
                 sum=sum+A[i][j]*x[j];
                  }
              x[i]=(A[i][n+1]-sum)/A[i][i];
         }

    printf("\n X = %f",x[0]);
    printf("\n Y = %f",x[1]);
    printf("\n Z = %f",x[2]);
   
      return(0);
}
i use this code for elimination on matrix, but i need to input 4 times at the first row even though i only put 3 scanf
what should i do to make it only need 3 input
thanks for replying
 
Physics news on Phys.org
Does your problem statement require the use of scanf() ? You may know that scanf returns an int - you have to compare the int returned against EOF (# a define in stdio.h). This checks for a matching failure. Also note that what is already in the keyboard buffer carries over to the next scanf call, e.g., the return key.

Decent programs try to avoid having multiples of the same statement over and over again. Why? Because it is harder to make changes in 20 lines of code than to change (and understand) 5 lines that do the same. Think loop.
 
Try removing the "\n" in the scanf calls, for example scanf("%f",&A[0][2]); without the "\n". scanf() is supposed to skip past whitespace on each call.
 
  • Like
Likes   Reactions: darwinharianto
jim mcnamara said:
Does your problem statement require the use of scanf() ? You may know that scanf returns an int - you have to compare the int returned against EOF (# a define in stdio.h). This checks for a matching failure. Also note that what is already in the keyboard buffer carries over to the next scanf call, e.g., the return key.

Decent programs try to avoid having multiples of the same statement over and over again. Why? Because it is harder to make changes in 20 lines of code than to change (and understand) 5 lines that do the same. Think loop.
yes i need the scanf because i have to input numbers on the matrix

rcgldr said:
Try removing the "\n" in the scanf calls, for example scanf("%f",&A[0][2]); without the "\n". scanf() is supposed to skip past whitespace on each call.
thank you
when i removed the "/n" it does solve the problem
 
C has looping features.

scanf("%f",&A[0][0]);
scanf("%f",&A[0][1]);
scanf("%f\n",&A[0][2]);
// through magic, increase a number by 1.
scanf("%f",&A[1][0]);
scanf("%f",&A[1][1]);
scanf("%f\n",&A[1][2]);
// through magic, increase a number by 1.
scanf("%f",&A[2][0]);
scanf("%f",&A[2][1]);

Is really ugly. What if you had a 4 x 4 ? 6 x 6 ?
 
  • Like
Likes   Reactions: darwinharianto
mafagafo said:
C has looping features.

scanf("%f",&A[0][0]);
scanf("%f",&A[0][1]);
scanf("%f\n",&A[0][2]);
// through magic, increase a number by 1.
scanf("%f",&A[1][0]);
scanf("%f",&A[1][1]);
scanf("%f\n",&A[1][2]);
// through magic, increase a number by 1.
scanf("%f",&A[2][0]);
scanf("%f",&A[2][1]);

Is really ugly. What if you had a 4 x 4 ? 6 x 6 ?
so it is better if i scanf the "n" and put it into a loop
if

so my loop will be like this?
Code:
for (i=0; i<=n; i++)
          for (j=0; j<=n; j++)
          scanf("%f";&A[i][j][I]);
for my input?[/I]
 
Supposing you code works (not going to compile and check for you), yes, this is the way to go. See how simpler and easier to refactor it got?

Note that if n is the dimension of the square matrix (3, for instance), you want i < n as 3 would refer to the fourth row or column.

Good luck.
 
mafagafo said:
Supposing you code works (not going to compile and check for you), yes, this is the way to go. See how simpler and easier to refactor it got?

Note that if n is the dimension of the square matrix (3, for instance), you want i < n as 3 would refer to the fourth row or column.

Good luck.
yes
sorry about that
i forgot it go n-1
thanks
 
mafagafo said:
Supposing you code works (not going to compile and check for you), yes, this is the way to go. See how simpler and easier to refactor it got?

Note that if n is the dimension of the square matrix (3, for instance), you want i < n as 3 would refer to the fourth row or column.

Good luck.
okay i have done it all, but i can't display them at the end of the code
i use this
Code:
    for(i=0; i<n+1; i++)
    printf("\n X[i] = %f",x[i]);
how can i make it display x1, x2 ... xn ?
 
  • #10
Supposing that from a bidimensinoal array you want something like:
\begin{array}{cc} a_{1, 1} &amp; a_{1, 2} \\ a_{2, 1} &amp; a_{2, 2} \end{array}

You should iterate i and j and get
Code:
a[j][i]

Placing ", " between terms on the same row (same value of j) and "\n" after the last term of a row.
 
  • #11
mafagafo said:
Supposing that from a bidimensinoal array you want something like:
\begin{array}{cc} a_{1, 1} &amp; a_{1, 2} \\ a_{2, 1} &amp; a_{2, 2} \end{array}

You should iterate i and j and get
Code:
a[j][i]

Placing ", " between terms on the same row (same value of j) and "\n" after the last term of a row.
maybe you get me wrong,
i mean i want to display X1 X2, ... Xn
not the full matrix
so the results will be like
X1 = ...
X2 = ...
X3 = ...
X4 = ...
for 4x4 matrix
 
  • #12
Yes, I got you wrong. So why are you having trouble with that?

C:
for (int i = 0; i < 4; i++) {
  printf("X%d = %d\n", i + 1, a[row_index - 1][I]);
}

Is this what you want?
 
  • Like
Likes   Reactions: darwinharianto
  • #13
mafagafo said:
Yes, I got you wrong. So why are you having trouble with that?

C:
for (int i = 0; i < 4; i++) {
  printf("X%d = %d\n", i + 1, a[row_index - 1][I]);
}

Is this what you want?
ahh, yes
thank you so much
 
  • #14
No problem. Are you new to programming? Why are you using C?
 
  • #15
mafagafo said:
No problem. Are you new to programming? Why are you using C?
yeah i am new to C
just a project for my test
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
Replies
9
Views
2K