Read from file - 1-digit and 2-digit numbers

This is a discussion on Read from file - 1-digit and 2-digit numbers within the C Programming forums, part of the General Programming Boards category; Hi all, I am writting a program which should from file read numbers (in this case 1-digit and 2 -digit) ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    Read from file - 1-digit and 2-digit numbers

    Hi all,
    I am writting a program which should from file read numbers (in this case 1-digit and 2 -digit) and then count how many times which number fall...in file from which program should read numbers are written like this... 1 2 3 4 5 6 7 8 9 10 11 12
    and result after compiling and running is 1 2 3 4 5 6 7 8 9 10 11 12(it writes them all good)
    but when he is counting...there are mistakes...
    num 1:2
    num 2:1
    num 3:1
    num 4:1
    num 5:1
    num 6:1
    num 7:1
    num 8:1
    num 9:0 (all others are 0)

    problem is that he si not counting them all...and he is not counting 2-digit numbers right...for example if we put first 9 numbers...he will count good first 8...9.number does not exist for him...and the second problem is that for example 12 he is not counting like 2-digit number...but as two 1-digit numbers 1 and 2....i dont know how to solve this...thanks for help....

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
    FILE *inputfile;
    int c,d;
    int b[21]={0};
    
    inputfile=fopen("input1.txt","r");
    
    if(inputfile == NULL) {
    printf("I cant open file \n");
    exit(1);
    }
    
    
    while((c=getc(inputfile))!=EOF ) {
    printf("%c",c);
    for(d=1;d<21;d++){
    if(c==d+49) b[d]++;
    }
    }
    
    printf("\n\n 1: %d  \n 2: %d  \n 3: %d  \n 4: %d  \n 5: %d  \n",b[1],b[2],b[3],b[4],b[5]);
    printf(" 6: %d  \n 7: %d  \n 8: %d  \n 9: %d  \n10: %d  \n",b[6],b[7],b[8],b[9],b[10]);
    printf("11: %d  \n12: %d  \n13: %d  \n14: %d  \n15: %d  \n",b[11],b[12],b[13],b[14],b[15]);
    printf("16: %d  \n17: %d  \n18: %d  \n19: %d  \n20: %d  \n",b[16],b[17],b[18],b[19],b[20]);
    
    fclose(inputfile);
    
    getch();
    return 0;
    }

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Wow. This other person is asking the same question.

    Read from file - 1-digit and 2-digit numbers - Dev Shed
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You need to treat the numbers as *NUMBERS*, not as individual digits.

    There's a very easy way to just bin the numbers, and have your answers - short and sweet. Start thinking about numbers, as numbers, and if I gave you numbers (not digits), one at a time, and you were putting each number into a mailbox - with each number having it's own mailbox - then what would you have after you had "binned" all the numbers, and looked at the contents of each mailbox?

    There you go.

    Your code is very close, just drop the "digit" treatment - that part is rubbish, imo.
    Last edited by Adak; 03-05-2010 at 05:57 PM.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Read from file - 1-digit and 2-digit numbers

    you can follow these steps,

    * Get the numbers.
    * find the digit for that number like,
    Code:
     if(num==0)
                    digit=1;
            else
            for(digit=0;num!=0;num/=10,digit++);
                    printf("count:%d\n",digit);
    you can have is code in one function.

    * Then you can increment count in one array like,
    Code:
    count[digit]++;
    * Finally if you print that array. It will have all the digits count.

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    By make the number as a string we can easily find its length ,
    so I believe that the following code will give you an idea to achieve your requirement
    Code:
     while(scanf("%d",&c))
            {
    
                    sprintf(str,"%d",c);
                    fprintf(stderr,"char :%d length : %d\n",c,strlen(str));
            }
    Note:
    I think you expect the Max length of a number is 20 , so you cannot store a length of a number 20 since INT_MAX is 2147483648
    that means MAX length must be 10.
    Last edited by Alexander jack; 03-05-2010 at 10:11 PM.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    while(scanf("%d",&c))
    scanf returns number of items successfully parsed or EOF. In the last case the condition will be true but no data to process...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    thank you for help adak,sganesh,Alexander jack,vart...
    first adak thank you for your great idea and tips...i am really trying to put it in code...i know that my code is close...and it works for 1-digit...
    second sganesh and Alexander jack if i got you right we misunderstand...i dont want to count how many times there was 1-digit and how many times 2-digit...i want to count number 1 as 1,number 2 as 2,number 12 as 12 not as only 2-digit....so my max length of number is 2-digit...there are number to 20...hope you understand me...

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: Read from file - 1-digit and 2-digit numbers

    Do you want to count how many times number 1 occurs in the file?
    If it is your requirement you can solve easily.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include<string.h>
    int main() {
    FILE *inputfile;
    int c,d=1,i=0;
    int b[21]={0};
    //char num[20];
    int len;
    inputfile=fopen("number","r");
    
    if(inputfile == NULL) {
    printf("I cant open file \n");
    exit(1);
    }
    
    
    int num;
    while(!feof(inputfile))
    {
    fscanf(inputfile,"%d",&num);
            b[num]++;
            num=0;
    }
    
    for(i=1;i<=20;i++)
    printf("%d: %d\n",i,b[i]);
    
    fclose(inputfile);
    
    return 0;
    }
    If it is wrong Tell me your requirement clearly.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Well, this is the whole basis of what you need, from Sganesh:

    Code:
    int num;
    while(!feof(inputfile))
    {
    fscanf(inputfile,"%d",&num);
            b[num]++;
            num=0;
    }
    feof() is not always reliable, but you can easily replace that with
    Code:
    while(ok = (fscanf(inputFile, %d", &num)) > 0) {
      b[num]++;
    }
    This is called "bin sorting", "binning", or "bucket sorting", but it's all very simple in concept, uses very little code, and runs faster than any other "sorter", (since it makes no comparisons, and has the smallest loop possible) - 1 line, involving just one increment.

    Note that with a huge range of numbers - say 0 to 50 Million, it would be impossible to use in this manner, since array's on a PC could not hold that many numbers. There are other structures that can be used, however: including sparse arrays, and skip lists.

    Remember this "binning" trick, because it can be used to advantage elsewhere.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21