Results 1 to 4 of 4

Thread: How to count numbers from a table in C language

  1. #1
    Join Date
    Aug 2009
    Posts
    59

    How to count numbers from a table in C language

    I am trying to count values in a table. I read a table with numerical values 1,1,3,1,45,5,12,36,1,2,45,5,5,12,88 etc. by example and I want in my program (C language) count of 1, 3, 5 etc. How can I count these numbers?

  2. #2
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to count numbers from a table in C language

    The problem is that in C we do not necessarily know the length of the table unless it is passed as a parameter to the function. Once you know the length of the table, there is more especially difficulty a counter or table counter, hop on and all that. You do a test when you read a number.

  3. #3
    Join Date
    Aug 2009
    Posts
    59

    Re: How to count numbers from a table in C language

    I considered this solution but it requires "asking" and test number. I tried this code but I do have a problem with array size. Or 5 files under /tmp, bearing the following names: J0, J1, J2, J3, J4, J5. Each file is written the following values:
    18
    12

    The source code:

    Code:
    #include <stdlib.h> 
    #include <stdio.h> 
    #define SIZE 4 
    
    int main() 
    { 
    int j, cpt, number1, number2; 
    int obs; 
    int tab[10]; 
    char filename[3]; 
    FILE *file; 
    
    for(j = 0; j <= SIZE; j++) 
    { 
    sprintf(filename,"/tmp/J%d",j); 
    if ((file = fopen ( filename, "r" )) == NULL) 
    { 
    perror ("Impossible to open the file."); 
    } 
    else 
    { 
    while ( fscanf ( file, "%d %d", &number1, &number2 ) == 2) 
    tab[j] = number2; 
    printf("tab[%d] = %d\n", j, tab[j]); 
    fclose (file); 
    } 
    } 
    printf("Number from? "); 
    scanf("%d",&cpt); 
    
    obs = 0; 
    for(j = 0; j <= SIZE; j++) 
    { 
    printf("tab[%d] = %d\n", j, tab[j]); 
    if(tab[j] == cpt) 
    { 
    obs = obs + 1; 
    } 
    } 
    printf("Total : %d\n", obs); 
    return EXIT_SUCCESS; 
    }

    The output is:

    tab [0] = 12
    tab [1] = 12
    tab [2] = 12
    tab [3] = 12
    array [4] = 12
    Number from? 12
    tab [0] = 877,277,040
    tab [1] = 0
    tab [2] = 12
    tab [3] = 12
    array [4] = 12
    Total: 3
    Why is my picture change?

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to count numbers from a table in C language

    When you have a table, regardless of the language in any loop route table is used myArray.length () or sizeof (myArray).

    After we are not obliged to declare a variable that, in general, the compiler is smart enough to avoid a calculation in each loop

Similar Threads

  1. How to change negative numbers to positive numbers in Excel
    By Shaina Na in forum Microsoft Project
    Replies: 3
    Last Post: 08-01-2012, 05:35 PM
  2. Link a Table to another Table to Drop Down In Main Table
    By himeshRES in forum Windows Software
    Replies: 6
    Last Post: 11-12-2010, 02:01 PM
  3. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  4. How can i count numbers on odd/even places?
    By cowa in forum Software Development
    Replies: 1
    Last Post: 17-11-2009, 08:43 AM
  5. SQL Query for Searching Missing Numbers from Sequence of Numbers
    By Bhagwandas in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 01:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,447,099.80009 seconds with 16 queries