c - Performing Operations on an Array -


ok, i'm new both programming , posting on site , question kind of complicated, i'm going attempt articulate situation. i'm trying write code read file (a list of polar coordinates) of unknown size, store values in array, perform conversion , out new array converted data (technically i'm using new array in predesigned function graph data in c++, not important since it's part of assignment , not source of error).

i'm having 2 major issues, first of variable array size. in code following, can see there's no constraint on x , y arrays, loop goes on convert values x[999] if there's nothing in r[] beyond r[40]. can constrain size of r , theta arrays because can code stop reading values when fscanf function reaches end of file, don't know analogy x , y arrays. (also, i'd appreciate if explain why character array doesn't have issue, though it's defined 128 characters. might give me insight need)

secondly, conversion doesn't quite match up. when print converted values, function converts them correctly, stores value converted r[i] , theta[i] x[i+1] , y [i+1]. have no idea causing problem.

i hope issues make sense. appreciated! if unclear, please let me know , i'll best explain. thank you!

#include <stdio.h> #include <math.h> #define arraysize 1000 #define filenamesize 128  float convertx(float r[], float theta[], int numpoints); float converty(float r[], float theta[], int numpoints);  int cmain()    {     int  i;     float r[arraysize], theta[arraysize], x[arraysize], y[arraysize];     char filename[filenamesize];  file *inputfile;  printf("type data file name: "); scanf("%s", filename);   inputfile = fopen(filename,"r");  if(inputfile == null){     printf("error, not open file: %s.\n",filename);     return 1; }  for(i=0; < arraysize; i++){     if(fscanf(inputfile,"%f %f", &r[i], &theta[i]) == eof) break;     printf("value %d %f\n",i+1,r[i]); }  fclose(inputfile);  for(i=0; < arraysize; i++){     x[i] = convertx(r,theta,i);     printf("value %d %f\n",i+1,x[i]); }  for(i=0; < arraysize; i++){     y[i] = converty(r,theta,i); }  return 0; }  float convertx(float r[], float theta[], int numpoints){     int i;     float x;     for(i=0; < numpoints; i++)         x = r[i]*cos(theta[i]);     return x; }  float converty(float r[], float theta[], int numpoints){     int i;     float y;     for(i=0; < numpoints; i++)         y = r[i]*sin(theta[i]);     return y; } 

edit: requested snippet of input file. here's first few points (there no commas in file, white space. unfortunately, formatting skills on site awful):

60, 3.9269875

60, 0.7853975

0, 0

1, 0.25

2, 0.5

for first issue, reason for-loop r , theta stops @ end of file because have break; exits for-loop when detect eof. don't have in x , y for-loops.

to fix that, instead of using arraysize constant, edit: { save final value in r , theta for-loops, either numbers of lines or +1, , use end value in x , y for-loop. this:

for(i=0; < arraysize; i++){     if(fscanf(inputfile,"%f %f", &r[i], &theta[i]) == eof) {       int final = i;       break;     }     printf("value %d %f\n",i+1,r[i]); }  fclose(inputfile);  for(i=0; < final; i++){     x[i] = convertx(r,theta,i);     printf("value %d %f\n",i+1,x[i]); } 

i said can use size of array r or theta: for (i=0; < r.size(); i++) {, since initialized length 1000 won't work. }

for 2nd issue: think if rid of for-loop in convertx , converty functions, solve problem. that's because last value of x or y calculated returned, calculations i=0 i=(numpoints-2) being thrown away.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -