Jump to content


Check out our Community Blogs

Imbellis

Member Since 09 Nov 2008
Offline Last Active Dec 02 2008 11:40 AM
-----

Topics I've Started

two-dimentional arrays allowing for user input

02 December 2008 - 11:21 AM

Hi there. I'm trying to make a program that allows the user to enter a 3x3 matrix but I can't even get the input right at the moment :S

Heres what I've done so far:
int main (void) {
	int status=0, i=0, j=0;
	int x[3][3];
	char line[100];
    
	      printf("Enter 3 x 3 matrix:\n");
		  
		      for(i=1; i<=3; i++) {
				  printf("Enter points of row %d:\n", i); 
				  for(j=0; j<=2; j++) {
					  while(1) {
					  
				      fgets(line, sizeof(line), stdin);
                      status = sscanf(line, "%d", &x[i][1+j]);
				      if (status==0) printf("Invalid point.\n");
					  else break;
				  }
			  }
		  }                   	  
	
	      printf("Data input:\n\n");
	     
		  for(i=1; i<3; i++) { 
			  for(j=0; j<2; j++) {
			  printf("%d\n", x[i][1+j]);
			  }
		  }
  
  return 0;
}

It needs to calculate the transpose of the matrix too, but actually getting the thing to input properly is my major concern right now.

Help please!

hi guys :)

09 November 2008 - 07:06 AM

Hello everyone. I'm a Physics student at Exeter University in England and I'm learning C for a Scientific Computing module I'm doing this semester. Already having a fair few problems (like the one I just posted in the C forum) and feeling a little overwhelmed by it all so I came here in search of aid!

Vector program problem

09 November 2008 - 06:54 AM

Hey there, I'm new here and I've only been coding for a little while for a course at university so forgive any stupid mistakes and such!

I was set the task of writing a program that inputs a series of 10 points from a text file, then outputting them and the distance between the first and the last points into another text file. That works fine, but they also want the distance between each point and the one before it to be displayed too.

So far my efforts have been largely useless so any help would be excellent :)

#include <stdio.h>
#include <math.h>
#define MAX_LINES 20

int main(void){ 
          FILE *InFile = (FILE *) NULL; /* Input file */ 
          FILE *OutFile = (FILE *) NULL; /* Output file */ 
          int NLines = 0; /* Actual number of lines read */ 
          char line[100]; /* To store one line of data */ 
          int Status = 0; /* To hold return value of file I/O functions */ 
          
          float pointx[MAX_LINES], pointy[MAX_LINES], pointz[MAX_LINES]; /* Array to store the other column */ 
          int i = 0; /* Counter variable for for loop */
          float r, d;
          
          InFile = fopen("DataIn.txt", "r"); /* Open input file */ 
                              
          while((NLines < MAX_LINES) && (!feof(InFile))) {/* read data, one line at a time */
                         fgets(line, sizeof(line), InFile); 
                         Status = sscanf(line, "%f %f %f\n", &pointx[NLines], &pointy[NLines], &pointz[NLines]); 
                         if (Status != 3) break; /* end of file reached */ 
                         NLines++; 
                         } 
                  OutFile = fopen("DataOut.txt", "w"); /* Open output file */ 
           



           for(i=0;i<NLines;i++) /* print out data */ 
           Status = fprintf(OutFile, "%f %f %f\n", pointx[i], pointy[i], pointz[i]); 
           
           r=sqrt(pow(pointx[i], 2) + pow(pointy[i], 2) + pow(pointz[i], 2))
           - sqrt(pow(pointx[i+1], 2) + pow(pointy[i+1], 2) + pow(pointz[i+1], 2));
           d=sqrt(r);


           fprintf(OutFile, "Distance between points: %f.\n", d);
           fprintf(OutFile, "Distance between points 1 and 10 is %f.\n", d);
           Status = fclose(InFile) + fclose(OutFile); /* Close files */ 


return 0; 
} 


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download