Question

I want to change this C code so that instead of prompting the user to enter...

I want to change this C code so that instead of prompting the user to enter the argument value, or requiring the argument be entered after by pressing the “enter” key, instead I want the code changed so that the program opens a file, (which include the input values) read in the elements and pass in the  input file as a command line argument. (the input file includes the size of the matrix and the matrix values)
 
Here's an example of the input file

3
2.3 2.7 3.0
6.5 7.36 14.8
14.1 10.6 -5.2

(where 3 is the size of the matrix followed by the matrix values)

I want this input file to be passed as a command line argument and produce the output instead of prompting the user to enter it manually.
please change following code so that it does that. thank you.
*This is a program to display the
upper triangular matrix and lower triangular matrix
for any given matrix. The method used here is
LU decomposition method. This program works for matrices
of order equal to or below 10x10*/
#include<stdio.h>
int main()
{
    int size,row,column,ctr1,ctr2;
    printf("ENTER THE SIZE OF THE MATRIX(size<10)\n");
    scanf("%d",&size);
    float matrix[10][10];
    printf("ENTER THE MATRIX\n");
    for(row=0;row<size;row++)
        for(column=0;column<size;column++)
            scanf("%f",&matrix[row][column]);
    float lowertriangle[10][10];//matrices for lower and uppper triangle
    float uppertriangle[10][10];
    for(row=0;row<size;row++)
        for(column=0;column<size;column++)
            {
                if(row>column)//initialise all elements of lower triangle in upper triangular matrix as 0
                     uppertriangle[row][column]=0.0;
                if(row<column)//initialise all elements of upper triangle in lower triangular matix as 0
                    lowertriangle[row][column]=0.0;
                if(row==column)//initialise all diagonal elements of lower triangular matrix as 1
                    lowertriangle[row][column]=1.0;
            }
/*The algorithm used later is from the book Introduction to algorithms by Thomas Cormen.For a better understaning one can
read this text under chapter Matrix Operations. */
    for(ctr1=0;ctr1<size;ctr1++)
    {
        uppertriangle[ctr1][ctr1]=matrix[ctr1][ctr1];
            for(ctr2=ctr1+1;ctr2<size;ctr2++)
            {
                uppertriangle[ctr1][ctr2]=matrix[ctr1][ctr2];
                lowertriangle[ctr2][ctr1]=matrix[ctr2][ctr1]/uppertriangle[ctr1][ctr1];
            }
             for(row=ctr1+1;row<size;row++)
                for(column=ctr1+1;column<size;column++)
                    matrix[row][column]=matrix[row][column]-lowertriangle[row][ctr1]*uppertriangle[ctr1][column];
    }
    printf("UPPER TRIANGULAR MATRIX\n");
    for(row=0;row<size;row++)
    {//displays upper triangular matrix
        for(column=0;column<size;column++)
            printf("%.2f ",uppertriangle[row][column]);
        printf("\n");
    }
        printf("LOWER TRIANGULAR MATRIX\n");
    for(row=0;row<size;row++)
    {//displays lower triangular matrix
        for(column=0;column<size;column++)
            printf("%.2f ",lowertriangle[row][column]);
        printf("\n");
    }
    getch();
}
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

/*This is a program to display the

upper triangular matrix and lower triangular matrix

for any given matrix. The method used here is

LU decomposition method. This program works for matrices

of order equal to or below 10x10*/

#include<stdio.h>

int main(int argc, char *argv[])

{

if(argc!=2){

printf("Usage: ./a.out in_file\n");

return -1;

}

FILE *fp;

fp = fopen(argv[1], "r");

if(fp==NULL){

printf("Unable to open file\n");

return -1;

}

int size,row,column,ctr1,ctr2;

// printf("ENTER THE SIZE OF THE MATRIX(size<10)\n");

fscanf(fp, "%d",&size);

float matrix[10][10];

// printf("ENTER THE MATRIX\n");

for(row=0;row<size;row++)

for(column=0;column<size;column++)

fscanf(fp, "%f",&matrix[row][column]);

fclose(fp);

float lowertriangle[10][10];//matrices for lower and uppper triangle

float uppertriangle[10][10];

for(row=0;row<size;row++)

for(column=0;column<size;column++)

{

if(row>column)//initialise all elements of lower triangle in upper triangular matrix as 0

uppertriangle[row][column]=0.0;

if(row<column)//initialise all elements of upper triangle in lower triangular matix as 0

lowertriangle[row][column]=0.0;

if(row==column)//initialise all diagonal elements of lower triangular matrix as 1

lowertriangle[row][column]=1.0;

}

/*The algorithm used later is from the book Introduction to algorithms by Thomas Cormen.For a better understaning one can

read this text under chapter Matrix Operations. */

for(ctr1=0;ctr1<size;ctr1++)

{

uppertriangle[ctr1][ctr1]=matrix[ctr1][ctr1];

for(ctr2=ctr1+1;ctr2<size;ctr2++)

{

uppertriangle[ctr1][ctr2]=matrix[ctr1][ctr2];

lowertriangle[ctr2][ctr1]=matrix[ctr2][ctr1]/uppertriangle[ctr1][ctr1];

}

for(row=ctr1+1;row<size;row++)

for(column=ctr1+1;column<size;column++)

matrix[row][column]=matrix[row][column]-lowertriangle[row][ctr1]*uppertriangle[ctr1][column];

}

printf("UPPER TRIANGULAR MATRIX\n");

for(row=0;row<size;row++)

{//displays upper triangular matrix

for(column=0;column<size;column++)

printf("%.2f ",uppertriangle[row][column]);

printf("\n");

}

printf("LOWER TRIANGULAR MATRIX\n");

for(row=0;row<size;row++)

{//displays lower triangular matrix

for(column=0;column<size;column++)

printf("%.2f ",lowertriangle[row][column]);

printf("\n");

}

getch();

}

Add a comment
Know the answer?
Add Answer to:
I want to change this C code so that instead of prompting the user to enter...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the...

    ****C PROGRAMMING**** Why isn't the determinant part of my code not running? I have included the attachments below and would greatly appreciate some help. ---------------------------------------            case 6:                printf("You chose determinate!\n\n");                printf("Press 2 for 2x2\n OR \n Press 3 for 3x3 Matrix: ");                scanf("%d", &detChoice);                printf("\n");                if (detChoice == 2) {                    printf("Matrix: ");...

  • I am trying to change my code so i no longer need the function that reads...

    I am trying to change my code so i no longer need the function that reads in the users base and height input but rather uses the implementation of argument vector in C. I want to accept command line arguments and have it calculate my area. so i want to type this into the command line and have it calculate my area project1 5 4 #include <stdio.h> void userDimensions(float *base, float *height) { scanf("%f", base); scanf("%f", height); } float calcArea(float...

  • System Programming in C

    Explain what the problem is within the program. Fix the problem when you create a child process per column. The code is given below. So basically, after the child processes have successfully excuted their code, the final print statement does not give the correct values. It prints the original matrix rather than the multiplied matrix.#include  #include  #include  #include  int main(int argc, char *argv[]){ int row = 0; int column = 0; row = atoi(argv[1]); column = atoi(argv[2]); int *A = ...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

  • Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float aver...

    Do you have a flowgorithim flow chart for this code? #include <stdio.h> int main() { int num,i=0,sum=0; float average; int customerNumbers[num]; int customerSales[num]; printf("How many customers do you want to track?\n"); scanf("%d",&num); while(i<num) { printf("Enter the customer number. "); scanf("%d",&customerNumbers[i]); printf("Enter the sales for the customer "); scanf("%d",&customerSales[i]); i++; } printf("Sales for the Customer"); printf("\nCustomer Customer"); printf("\nNumber Sales"); for(i=0;i<num;i++) { printf("\n %d \t %d",customerNumbers[i], customerSales[i]); sum=sum+customerSales[i]; } average=(int)sum/num; printf("\n Total sales are $%d",sum); printf("\n Average sales are $%.2f",average); printf("\n --------------------------------------------");...

  • Within this C program change the input to a file instead of individual input from the...

    Within this C program change the input to a file instead of individual input from the user. You must take the input file name on the command line. Be sure to have simple error checking to be sure the file exists and was successfully opened. The input file will have the following format: First line: Number of students Second Line: Number of grades Third Line: Student Names, space delimited Fourth+ Line(s): Grades for all students for one assignment, space delimited....

  • So I have a question in regards to my program. I'm learning to program in C...

    So I have a question in regards to my program. I'm learning to program in C and I was curious about the use of functions. We don't get into them in this class but I wanted to see how they work. Here is my program and I would like to create a function for each of my menu options. I created a function to display and read the menu and the option that is entered, but I would like to...

  • #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i);...

    #include "stdio.h" int main() { float array[5][3],rowsum=0.0,colsum=0.0; int i,j; for(i=0;i<5;i++){ printf("Enter the %d elements of array:\n",i); for(j=0;j<3;j++){ scanf("%f",&array[i][j]); } } printf("Given table data:\n"); for(i=0;i<5;i++){ for(j=0;j<3;j++){ printf(" %0.1f",array[i][j]); } printf("\n"); } for(i=0;i<5;i++){ for(j=0;j<3;j++){ rowsum=rowsum+array[i][j]; } printf("sum of the %d row is %0.1f\n",i,rowsum); rowsum=0; } for(j=0;j<3;j++){ colsum+=array[j][i]; printf("Sum of %d coloumn is %0.1f\n",j,colsum); colsum=0; } return(0); } can someone help me to get the correct row sum and column sum in c program

  • Debug the following matrix program in C: // Program to read integers into a 3X3 matrix...

    Debug the following matrix program in C: // Program to read integers into a 3X3 matrix and display them #include <stdio.h> void display(int Matrix[3][3],int size); int main(void) {         char size;         double Matrix[size][size+1];         printf("Enter 9 elements of the matrix:\n");         int i;         for (i = 0: i <= size: i++)     {       int j = 0;       for (; j <= size++; j++){         scanf("%d", matrix[i--][4])       }     }         Display(Matrix,9);         return 0; void...

  • Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int...

    Fix the errors in C code #include <stdio.h> #include <stdlib.h> void insertAt(int *, int); void Delete(int *); void replaceAt(int *, int, int); int isEmpty(int *, int); int isFull(int *, int); void removeAt(int *, int); void printList(int *, int); int main() { int *a; int arraySize=0,l=0,loc=0; int choice; while(1) { printf("\n Main Menu"); printf("\n 1.Create list\n 2.Insert element at particular position\n 3.Delete list.\n4. Remove an element at given position \n 5.Replace an element at given position\n 6. Check the size of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT