Jump to content

[C] Linked list Woes!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Scupham

Scupham

    Newbie

  • Members
  • Pip
  • 4 posts
I am not getting the correct output from my program.
However i know the problem is to do with me not terminating the linked list correctly in the reading function.

Yet i have no idea how to fix this and have spent the last 12 hours fiddling around with different code, alas no luck.

If anyone could show me how i need to setup my linked list that would be great!

Thanks in advance,
Scupham



#include<stdio.h>

#include<math.h>

#include<stdlib.h>



struct filenames

        {

        char measurements_filename[101] ;

        char line_parameters_filename[101];

        };


struct measurements

        {

        double x,y;

        struct measurements *next;

        };


struct line_parameters

        {

        double gradient, constant;

        struct line_parameters *next;

        };




int main (void)

{

struct filenames filenames, *filenames_ptr;

struct measurements measurements, *measurement_ptr;

struct line_parameters line_parameters, *line_parameters_ptr;

        

        void get_filenames(struct filenames *);

        void read_measurments(struct filenames*, struct measurements *);

        void calculate_line_parameters(struct measurements*, struct line_parameters * );

        void write_line_parameters(struct filenames, struct line_parameters );


filenames_ptr = &filenames;

measurement_ptr= &measurements;

line_parameters_ptr= &line_parameters; 



get_filenames(filenames_ptr);


read_measurments(filenames_ptr, measurement_ptr);

fprintf(stdout, "Measurements Read\n");


calculate_line_parameters(measurement_ptr, line_parameters_ptr);

fprintf(stdout, "Calculating Parameters\n");


write_line_parameters(filenames, line_parameters);

fprintf(stdout, "Gradient and Constant written to file.\n");

return(0);

}





void get_filenames(struct filenames *filenames_ptr)

{

fprintf(stdout, "Enter file name:\n");

fscanf(stdin, "%s",filenames_ptr->measurements_filename);


fprintf(stdout, "Enter file name to Save to:\n");

fscanf(stdin, "%s",filenames_ptr->line_parameters_filename);

return;

}





[B]void read_measurments(struct filenames *filenames_ptr, struct measurements *measurement_ptr)

{

char line[101];

char *line_ptr;

int no_values=0;

struct measurements *current_measurements_ptr=measurement_ptr;

FILE *input_stream;

input_stream=fopen(filenames_ptr->measurements_filename, "r");



if(input_stream!=NULL)

                {

                fprintf(stdout, "File found!\n");

				line_ptr= line;

                fgets(line,sizeof(line), input_stream);

                        

                        while (((line_ptr=fgets(line,sizeof(line), input_stream))!=NULL) && ((no_values= sscanf(line,"%lf %lf",

                                                                                ¤t_measurements_ptr->x,

                                                                                ¤t_measurements_ptr->y))==2))

                        {

                        current_measurements_ptr -> next=(struct measurements*)malloc(sizeof(struct measurements));                

                

                        if((line_ptr!=NULL) && (no_values!=2))

                        fprintf(stdout, "Error reading line %s \n", line);                

                        current_measurements_ptr -> next= NULL;        

                        

                        }

}        

fclose(input_stream);

return;

}[/B]

                                




void calculate_line_parameters(struct measurements *measurements, struct line_parameters *line_parameters_ptr)


{

struct measurements *measurement_ptr=NULL, *current_measurements_ptr;


double sumx=0, sumy=0, sumxx=0, sumxy=0, n=0;


current_measurements_ptr = measurements;


while(current_measurements_ptr->next != NULL)

        {


        sumx  +=  measurement_ptr->x;

        sumy  +=  measurement_ptr->y;

        sumxx += (measurement_ptr->x) * (measurement_ptr->x);

        sumxy += (measurement_ptr->x) * (measurement_ptr->y);

        

        n ++;

        current_measurements_ptr = current_measurements_ptr->next;

        }


line_parameters_ptr->gradient = ((n * sumxy)-(sumx * sumy))/((n * sumxx)-(sumx * sumx));

line_parameters_ptr->constant = ((sumxy * sumx) - (sumxx * sumy)) / ((sumx * sumx) -(n * sumxx));

return;

}




void write_line_parameters(struct filenames filenames, struct line_parameters line_parameters)

{

FILE *output_stream;

output_stream = fopen(filenames.line_parameters_filename, "w");


fprintf(stdout, "Gradient: %lf \n",line_parameters.gradient);        

fprintf(stdout, "Constant: %lf \n",line_parameters.constant);

fprintf(output_stream, "Gradient = %lf \n", line_parameters.gradient);

fprintf(output_stream, "Constant = %lf \n", line_parameters.constant);


fclose(output_stream);

return;

}


Input Data:

Quote

X Y
1.0 2.876
2.6 4.234
3.7 8.874
4.5 7.698
6.0 9.932

Current Ouput:

Quote

Gradient = -1.#IND00

Constant = -1.#IND00


#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Make sure you set current_measurements_ptr->next->next = NULL. This makes sure your new object is properly terminated rather than pointing at random data.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog