d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C Program > Help
Add Reply New Topic New Poll
Member
Posts: 2,081
Joined: Jan 13 2007
Gold: 515.76
Oct 14 2012 02:04pm
The first line of a file contains a positvie integer value, n. the file then contains n additional lines.
each line contains a pair of integers that represent the x and y coordinates of a point
the name of the file comes in as argv[1]
first read in the number n, then malloc up some space to form an array of n structs. each struct will contain 2 fields. a struct represents a point
then fill the array of structs with the n points- start filling at index 0 and go left to right
Sweep through the array and loate the index of the array where the lowermost then leftmost point is located. Refer to this as index j.
print out both the index of the array where the lowermost then leftmost point is located and print the x and y coordinates.

I'm not quite sure how to get this going.
All I have so far so any help is appreciated, thanks.


Code
#include <stdio.h>
#include <stdlib.h>

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

 struct point{ int x; int y; }point;

 FILE *fin;
 int n, i, lowest;

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

 fscanf( fin, "%d", &n );

 point = malloc( sizeof( point ) ); // malloc up space to form an array of structs

 for( i = 0; i < n; i++ ) {

   fscanf( fin, "%d%d", point.x, point.y );

   if( point[0].y < point[1].y )  point[0].y = lowest;
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Oct 14 2012 02:18pm
what are you having troubles with?

i think you almost have it if i understand the problem correctly

Code
typedef struct { int x; int y; } point;

main()
{
file ...
int n, i, lowers;
point *points;

fin = fopen
fscanf(n)

points = mallow(sizeof(point) * n); //malloc an array of points of n elements

for(){
fscanf(point point)
if(points[i].y < lowerst) points[i].y = lowerst; //compair the current elements value to lowest if lowers set
}
}


something like that i think only four major differences in this psudo code. i defined the struct as typedef struct {} name;

makes it much easier to use a struct because you dont have to define the variable as struct name new_name; you can just use name new_name;

next change is the malloc. if n is the number of lines in the file then you should multiple point by n to create enough space for n structs.

also changed point to a pointer. im not sure if you can use the . to access it or if you have to use => i cant recall right at the moment.

last change is to compair lowest to the current array that was written and write to lowest if the new struct array is lower

totally tired so this might not even make sense

This post was edited by AbDuCt on Oct 14 2012 02:19pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll