简体   繁体   中英

Performing Operations on an Array

Ok, so I'm new to both programming and posting on this site and my question is kind of complicated, but I'm going to attempt to articulate my situation. I'm trying to write a code to read a file (a list of polar coordinates) of unknown size, store the values in an array, then perform a conversion and get out a new array with the converted data (technically I'm using this new array in a predesigned function to graph the data in C++, but this is not important since it's part of the assignment and certainly not the source of error).

I'm having 2 major issues, the first of which is the variable array size. In the code following, you can see that there's no constraint on my x and y arrays, so the loop goes on to convert values up to x[999] even if there's nothing in r[] beyond r[40]. I can constrain the size of my r and theta arrays because I can code to stop reading values when the fscanf function reaches the end of the file, but I don't know the analogy for my x and y arrays. (Also, I'd greatly appreciate it if someone could explain why my character array doesn't have this issue, even though it's defined to be up to 128 characters. It might give me the insight I need)

Secondly, the conversion doesn't quite match up. So when I print the converted values, the function converts them correctly, but stores the value converted from r[i] and theta[i] to x[i+1] and y [i+1]. I really have no idea what is causing this problem.

I certainly hope my issues make sense. Any help would be appreciated! If anything is unclear, please let me know and I'll do my best to 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 the data file name: ");
scanf("%s", filename);


InputFile = fopen(filename,"r");

if(InputFile == NULL){
    printf("Error, could not open file: %s.\n",filename);
    return 1;
}

for(i=0; i < ARRAYSIZE; i++){
    if(fscanf(InputFile,"%f %f", &r[i], &theta[i]) == EOF) break;
    printf("Value %d is %f\n",i+1,r[i]);
}

fclose(InputFile);

for(i=0; i < ARRAYSIZE; i++){
    x[i] = convertx(r,theta,i);
    printf("Value %d is %f\n",i+1,x[i]);
}

for(i=0; i < 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; i < 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; i < numPoints; i++)
        y = r[i]*sin(theta[i]);
    return y;
}

EDIT: Requested was a snippet of the input file. Here's the first few points (there are no commas in the file, just white space. Unfortunately, my formatting skills on this site are awful):

60, 3.9269875

60, 0.7853975

0, 0

1, 0.25

2, 0.5

For your first issue, the reason that your for-loop for r and theta stops at the end of the file is because you have a break; that exits the for-loop when you detect EOF. You don't have that in your x and y for-loops.

To fix that, instead of using your ARRAYSIZE constant, EDIT: { save the Final i value in your r and theta for-loops, it will be either the numbers of lines or +1, and use that for the end value in your x and y for-loop. Like this:

for(i=0; i < ARRAYSIZE; i++){
    if(fscanf(InputFile,"%f %f", &r[i], &theta[i]) == EOF) {
      int final = i;
      break;
    }
    printf("Value %d is %f\n",i+1,r[i]);
}

fclose(InputFile);

for(i=0; i < final; i++){
    x[i] = convertx(r,theta,i);
    printf("Value %d is %f\n",i+1,x[i]);
}

I previously said you can use the size of the array for r or theta: for (i=0; i < r.size(); i++) { , but since you initialized its length to 1000 that won't work. }

For your 2nd issue: I think if you get rid of the for-loop in your convertx and converty functions, that will solve the problem. That's because only the last value of x or y calculated is returned, so your calculations from i=0 to i=(numPoints-2) are being thrown away.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM