me_roy 0 Newbie Poster

My guess is you're supposed to enter as many numbers as in the file, right? So after you read the file, set up another loop and input the numbers into another array. Now your float value [1] will correspond to entered integer [1].

Now, set up a loop to process each float.
Inside that loop, another loop to process the corresponding integer value.

That should do it.

And PLEASE fix your Formatting -- it's terrible.

You are right, the user should enter the number as many as number in the file.
Thank for the suggestion, kind hard to catch up for a novice like me. I think this novice situation, explained a lot my formatting terrible, i will learn to fix it. Thank WaltP :)

me_roy 0 Newbie Poster

If you require a dynamic solution, then consider an integer array to hold your digit values..

So far for generating the dynamic float number i used this code:

...
      int number; //represent numbers in the file
      float *si;
      ...
      si = malloc (number*sizeof(float));
      ...

it works. i might be wrong used this approach, since i not considered the dynamic number enter by user.
Thank for your suggestion gerard4143.

me_roy 0 Newbie Poster

Thank you for the nice code. It's almost solved the problem i present here.
I appreciate it. However, my original problem is more complicated.
One of them is that the user should able enter more than one digit value.

Another thing is, the number float data actually is dynamic, not just 4 float data as i present here, sometimes more than 100.
I realized, such condition will need "malloc" or "calloc".

Since the number of float data is dynamic, the user in particular condition will enter more than 4 number.

Looks a like every point i mention related to dynamic condition. Pheww..such hard problem for me.

I will try to adopt your code in my problem. Further suggestion and help are very welcome.

regards,

me

me_roy 0 Newbie Poster

Hi all,

I am stuck here after several hours tried.
Here the thing. I have txt file that contain float data (1 column and many row) such like this (4 float data):

0.799
0.851
0.926
1.000

Then i want to read it as array. My code is work until this point.
After read it as array, than the code should copy each data
as much as number enter by user.
The user should enter 4 different number, since there are 4 float data in the read txt file.
For example if user enter number 2,3,1,2 respectively, the code must copy the 0.799 two times, the 0.851 three times, and so on.
Then put in the different txt file.
In this case the contain of new txt file must:
0.799
0.799
0.851
0.851
0.851
0.926
1.000
1.000

My coding failed to do this task, instead copying each of data as accumulation number input by user. So in this case the accumulative value from 2,3,1,2 is 8 (2+3+1+2).
My result so far are:
0.799
0.799
...
0.799 (until 8 row, then)
0.851
0.851
...
0.851 (until 8 row..and so on )
Here the code:

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <string.h>

main() 
{
       FILE *fileread,*fileprint;
       int i,j, cp1, cp2, cp3, cp4;
       double *z;
       double wght[20],sic[20];
       


    if ( ( …
me_roy 0 Newbie Poster

Wow so many responds, i should try it one by one...thanks Guys..

Regards from novice and newbie in C

me_roy

me_roy 0 Newbie Poster

Dear All,

I got difficulties here related with extracting max and min value from every column in array.
So far i just can extract max-min value at a time by changing column index, stupid approach..eeeh, but i do it just for testing that code could extract right max and min value
for every column. But when i want to automatically got max and min values for every
different column, i got stuck..

Could you help me..please...

#include <stdio.h>
#include <io.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

main() 
{
     float M[3][3]=
     {
           {0.01, 1.02, 2.05},
           {2.01, 0.00, 5.00},
           {3.01, 4.00, 0.40}
     };
    
    float mx, mn;
    
    int i,j;    
    
     
    
    for (i=0;i<3;i++)
    {
     
     for (j=0; j<1; j++) // Here i changed the column for testing only.
                         // with j=0 and j<1 i just want to make sure that max = 3.01
                         // and min = 0.01.
                         // so when i changed j = 1 and j<2 i got max = 4.00, min 0.00
                {
                if(i==0)
                {mn = M[0][j];} 
                if (mx < M[i][j])
                {mx = M[i][j];}
                if (M[i][j] < mn)
                {mn= M[i][j];}
               
               }
                
     }    
    printf("min=%.2f + max=%.2f\n",mn,mx);
    system ("pause");	    
     return 0;
}

regards,

roy

me_roy 0 Newbie Poster

Ups sorry, if i did not well enough explain my problem. I will try it now:

Suppose i has the data file,
"array.dat", that contain like this:

100.01 8.02 7011.03 1.04 7.05
201.01 9.02 203.03 2.07 2.05
201.01 9.02 801.03 2.07 2.05
445.81 2.09 9090.04 5.09 901

the data should read first, and using some function
the data will consider as input (here as x), using this example
simple function: y=2.x-5,
every data point here should changed into new value,
than the result print into new file.

thanks a lot in advanced

me_roy 0 Newbie Poster

Seem my problem almost similar, but more complicated.

what if my input.txt consist of many column, than for every element there we want
to evaluate the value into new value?

for exampe the input.txt look like this:

123.34 1.02 2.02 3.04
342.54 0.01 12.3 0.19
243.43 0.99 11.23 1.345

thank you in advanced

me_roy 0 Newbie Poster

my problem is quiet similar, i used atof here my data and code:

"square10.dat"
0.01 1:0.1
0.04 1:0.2
0.09 1:0.3
0.16 1:0.4
0.25 1:0.5
0.36 1:0.6
0.49 1:0.7
0.64 1:0.8
0.81 1:0.9
1.00 1:1.0

THE CODE:

#include <stdio.h>

int main() {
  char c[10];  /* declare a char array */
  int i;
  double *z,tmp1;
  FILE *file;  /* declare a FILE pointer  */

  file = fopen("square10.dat", "r"); 
  /* open a text file for reading */

  if(file==NULL) {
    printf("Error: can't open file.\n");
    /* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
    return 1;
  }
  else {
    printf("File opened successfully. Contents:\n\n");
    
    i=0;
    while(fgets(c, 25, file)!= NULL) 
    { 
      z= strtok (c, " ");
      /* keep looping until NULL pointer... */
      printf("String c[%i]: %s\n",i,z);        
      /* print the file one line at a time  */
      float tmp1 = atof(z);
      printf("Convert c[%i]: %.6f\n",i,tmp1);
      i=i+1;
    }

    printf("\n\nNow closing file...\n");
    fclose(file);
    system("pause");
    return 0;
  }
}

my goal is to pick the the first float number 0.01;0.04...1.00 and calculate it with certain formula (the formula not provide yet here).
i try to separate it first from 2nd string using strtok.
Since in my opinion the first number is in string format than i used atof to convert it to float.
But the result is weird. Would you help me with this problem?

Here the result so far:
String c[0] = 0.01
Convert c[0] = 2293488.000000
String c[1] = …