Thread: Need advise

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    58

    Need advise

    Hello! I will write program in which you should enter 20 or more real numbers write them into .dat file and program should read from that .dat file and sort first 20 numbers, also sum all the number. I need advise or maybe if you have some example what is the bet way to do it because i tried few methods but **** happens.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is your best attempt?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    I only started to write into file and read what i wrote but i am getting 0,000000 instead of number i entered so i can't do following actions :/
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main()
    {
    FILE *fp;
    int i=1;
    char numbers[200];
       
        
    if ( (fp=fopen("numbers.dat","wb+"))==NULL)
    {
         printf ("No such file \n");
         system("pause");
         exit(1);
    }
    printf ("Enter number \n");
    printf ("End of input ctrl+z \n");
    
    while (!feof(stdin))
    {
          printf("Enter %d  number : \n",i);
          fscanf(stdin,"%f",&numbers[i]);
          fseek( fp, i *  sizeof( float ), SEEK_SET );
          fwrite(&numbers[i], sizeof(float),1,fp);
          i++;
    }
    i=0;
    rewind(fp);
    printf("Number you entered : \n");
    while (!feof(fp))
    {
          fread(&numbers[i], sizeof(float),1,fp);
          printf("%f ", numbers[i]);
          i++;
    }
    system("pause");
    }
    P.S. It works only with integer but not with float

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your main problem is that you've defined numbers as a char array whereas if it's going to hold floats it should be of type ... float.

    Also, your loop control is not correct. Try something like this (and note that you don't need the fseek since the file pointer will automatically advance after each fwrite).
    Code:
    while (1) {
          printf("Enter %d  number : \n",i);
          if (fscanf(stdin, "%f", &numbers[i]) != 1)
              break;
          fwrite(&numbers[i], sizeof(float), 1, fp); // should actually check return value here too!
          i++;
    }
    i=0;
    rewind(fp);
    printf("Number you entered : \n");
    while (1) {
          if (fread(&numbers[i], sizeof(float), 1, fp) != 1)
              break;
          printf("%f\n", numbers[i]);
          i++;
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Thank you. One more question. If there is possibility in this case to sort numbers without storing them into array?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krakatao
    If there is possibility in this case to sort numbers without storing them into array?
    You need to store them somewhere to sort them. Like, if I read out a list of numbers to you and ask you to sort them, you won't be able to do so if you forget each number as soon as I read the next one to you.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Yes i understand. I mean not storing all file into array. Just those elements which i need to compare

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Still cannot find a way to sort first 20 numbers. I tried something like this :/
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    FILE *fp;
    int i=1,k,j;
    float numbers,average=0,now,temp,minimum;
    
       
        
    if ( (fp=fopen("numbers.dat","wb+"))==NULL)
    {
         printf ("No such a file \n");
         system("pause");
         exit(1);
    }
    printf ("Enter number\n");
    printf ("End ctrl+z \n");
    
    while (!feof(stdin))
    {
          
          printf("Enter %d number : \n",i);
          fscanf(stdin, "%f", &numbers);
          if(feof(stdin))
          {
          break;
          }
          fwrite(&numbers, sizeof(float),1,fp);
          i++;
    }
    i=0;
    rewind(fp);
    printf("Numbers you entered : \n");
    while (1)
    {
          if (fread(&numbers, sizeof(float), 1, fp) != 1)
          break;
          printf("%f ", numbers);
          average=average+numbers;
          i++;
    }
    average=average/i;
    system("pause");
    rewind(fp);
    /*SORTING*/
    for (j=1; j<=20; j++)
    {
    minimum=fread(&numbers, sizeof(float), 1, fp);
    for (k=j; k<=20; k++)
    {
        now=fread(&numbers, sizeof(float), 1, fp);
        if (now<minimum)
        {
        temp=0;
        temp=minimum;
        minimum=now;
        fseek(fp,j*sizeof(float),SEEK_SET);
        fwrite(&minimum, sizeof(float),1,fp);
        }
    }
        fseek(fp,k*sizeof(float),SEEK_SET);
        fwrite(&temp, sizeof(float),1,fp);
        
    }
    /*SORTING*/
    rewind(fp);
    while (1)
    {
          if (fread(&numbers, sizeof(float), 1, fp) != 1)
          break;
          printf("%f ", numbers);
    }
    system("pause");         
    
    
    }

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    There are a number of well known, common sorting algorithms:

    Sorting algorithm - Wikipedia, the free encyclopedia

    The first three are probably the simplest. Insertion sort might apply best to what you are doing, because it involves reading in one number at a time and inserting it into an array.

    Pick one, try to implement it, and if it doesn't work out, you can say, eg, "What's wrong with my bubblesort?".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by MK27 View Post
    There are a number of well known, common sorting algorithms:

    Sorting algorithm - Wikipedia, the free encyclopedia

    The first three are probably the simplest. Insertion sort might apply best to what you are doing, because it involves reading in one number at a time and inserting it into an array.

    Pick one, try to implement it, and if it doesn't work out, you can say, eg, "What's wrong with my bubblesort?".
    The problem is i can't print out floats from array(it prints out smiles instead of numbers).

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by krakatao
    The problem is i can't print out floats from array(it prints out smiles instead of numbers).
    Well, what line of code did you use to "print out floats from array". Also, show the declaration of the array.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by laserlight View Post
    Well, what line of code did you use to "print out floats from array". Also, show the declaration of the array.
    Code:
    array[0]=fread(&numbers, sizeof(float), 1, fp);
    printf("%s \n",array);
    system("pause");
    }

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Code:
    char array[200];

  15. #15
    Registered User
    Join Date
    Nov 2011
    Posts
    58
    Quote Originally Posted by laserlight View Post
    What is array?
    Code:
    char array[200];

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advise for the day.
    By Mario F. in forum General Discussions
    Replies: 8
    Last Post: 04-01-2010, 03:51 PM
  2. Career Advise
    By ggraz in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 10-23-2008, 03:57 AM
  3. please advise....
    By manny in forum C Programming
    Replies: 2
    Last Post: 06-07-2006, 06:09 AM
  4. Need some advise
    By ILoveVectors in forum C++ Programming
    Replies: 5
    Last Post: 06-21-2005, 09:24 AM
  5. Some Advise..
    By VaSt in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2003, 01:52 PM