Thread: array help

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    104

    Question array help

    okI'm starting out small so please bear with me. This is what I have to do. C program that will accept up to 50 validated integers from the keyboard (a null entry terminates the input process), store the integers in a subscripted array, sort the array in ascending sequence, copy the sorted values to a linked list organized with pointers, and then display the values on the screen. so i have broken it into 6 step. So the first on i want to do is to input the intergerts into an array. I have lowed the number in the array to 5. So i need to know what i did wrong to get it into my array the first time

    Code:
     #include <stdio.h>
    
    #define Max 5 //number to set array
    
     main ()
    {
         int Num[Max]; // numbers to be added to array
       
       printf ("please enter 5 random nunber");
       scanf ("%d", &Num);
       
       while (Num == NULL)
       {
         printf ("please enter 50 random nunber");
         scanf ("%d", Num);
         }
        printf ("%d",  Num);   
     
     getchar();
     return 0;    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Have you read the FAQ on how to get numbers from the user? It sounds like you have been incorrectly told what NULL is (I believe there is an FAQ on that too). Please don't tell me that you have to use scanf.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    no i was trying to make it a little more simple for me to see that it was reading the data. I only have to print after doing all of those other steps and an not to sure how to do that. I do know how to get data from a user just i'm not to sure i read it into an array that i am writing that part right so that is why i out the scanf. Is there a better way to do it.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Last edited by Dave_Sinkula; 09-21-2006 at 09:04 PM. Reason: Added/edited links.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok this is what I have come up with to do the progam listed above. Im getting the following error when i try to debug any ideas on what i did wrong.
    E:\tmitchellwk3.cpp In function `int main()':
    58 E:\tmitchellwk3.cpp invalid conversion from `int' to `int*'
    58 E:\tmitchellwk3.cpp initializing argument 1 of `void ArraySort(int*, int (*)(int, int), unsigned int)'

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 5 //number to set array
    
    typedef int (*CMPFUN)(int, int);
    
    void ArraySort (int i[], CMPFUN fun_prt, unit32 ub)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = i[indx2];
                          temp2 = i[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              i[indx2 -1] = temp;
                              i[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
       }
       
       ArraySort(i, cmpfun, Max);
    
       for (i = 0; i < Max; i ++)
            printf ("%d\n",  Num[i]);
    
       getchar();
       getchar();
       return 0;    
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    The first parameter to your ArraySort function should be an array not an integer.
    Num is the name of your array.


    Code:
    for (i = 0; i < Max && Num[i] != 0; i++)
    Num[i] will always be uninitialized. You don't set it until after this comparison.


    Code:
       
    ArraySort(i, cmpfun, Max);
    
    for (i = 0; i < Max; i ++)
            printf ("%d\n",  Num[i]);
    You may have read in fewer than Max number of records
    Last edited by spydoor; 09-22-2006 at 02:01 PM.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    can you tell me
    that how i wrote this meats these items
    program that will accept up to 50 validated integers from the keyboard (a null entry terminates the input process), store the integers in a subscripted array, sort the array in ascending sequence, copy the sorted values to a linked list organized with pointers, and then display the values on the screen. so i have broken it into 6 step. So the first on i want to do is to input the intergerts into an array. I have lowed the number in the array to 5. So i need to know what i did wrong to get it into my array the first time
    Code:
     
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 5 //number to set array
    
    typedef int (*CMPFUN)(int, int);
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
       }
       
      
     
       for (i = 0; i < Max; i ++)
       {
           ArraySort(Num, cmpfun, Max);
            printf ("%d\n",  Num[i]);
    }
       getchar();
       getchar();
       return 0;    
    }

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
       }
    You still don't initialize Num[] and so testing its contents are a bad idea. You'll want to test Num[i] against zero after you read it from the user, perhaps with if(...) break;.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok so that would be why i can only enter in eight number and then it prints. I also want to be able to stop entering numbers before i get to 50 but do not expect no more then 50 how would i do that
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit60 unsigned int 
    #define Max 30 //number to set array
    
    typedef int (*CMPFUN)(int, int);
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit60 ub)
    {  //start bubble array
       
               unit60 indx;
               unit60 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
       }
       
      
     
       for (Num[i] = 0; i < Max; i ++)
       {
           ArraySort(Num, cmpfun, Max);
            printf ("%d\n",  Num[i]);
    }
       getchar();
       getchar();
       return 0;    
    }

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
    Your array is uninitialized. Which really means that you probably luck out. Otherwise, your check is wrong. You're trying to check Num[i] before you ever actually put anything into it. Remove that portion of the check, add an if check to see if you've just entered zero, and break.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    main ()
    {
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max && Num[i] != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
           if(Num[i] == 0) break;
       }
    • Get rid of the code in red.
    • Add in the code in green.

    You can't test a number before you set it to something. So, you could do as I suggested, or set each element of the array to a number other than zero before the loop.

    [edit] Quzah beat me by three minutes, probably because I was too busy adding colour. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    ok when i have my code as
    Code:
      
    #include <stdio.h>
    #include <stdlib.h>
    
    #define unit32 unsigned int 
    #define Max 50 //number to set array
    
    typedef int (*CMPFUN)(int, int);
    
    void ArraySort(int Num[], CMPFUN fun_prt, unit32 ub)
    {  //start bubble array
       
               unit32 indx;
               unit32 indx2;
               int temp;
               int temp2;
               int flipped;
               
               if (ub <= 1)
                return;
                
                indx =1;
                do
                { 
                      flipped = 0;
                      for (indx2 = ub-1; indx2 >= indx; --indx2)
                      {
                          temp = Num[indx2];
                          temp2 = Num[indx2 -1];
                          if ((*fun_prt)(temp2,temp)>0)
                          {
                              Num[indx2 -1] = temp;
                              Num[indx2] = temp2;
                              flipped =1;
                           }
                      }
                }while ((++indx < ub) && flipped);
    }
    
    int cmpfun (int a, int b)
    {
        if (a > b)
         return 1;
        else if (a < b)
          return -1;
         else
           return 0;
    }                  
    main ()
    {
       int Num[Max]; // numbers to be added to array
       int i;
      
       for (i = 0; i < Max != 0; i++)
       {
           printf ("Please enter a random nunber : ");
           scanf ("%d", &Num[i]);
           if(Num[i] == 0) break;
    
       }
       
      
     
       for (i = 0; i < Max; i ++)
       {
           ArraySort(Num, cmpfun, Max);
            printf ("%d\t",  Num[i]);
    }
      printf ("HIT ENTER TO EXIT\n");
       getchar();
       getchar();
       return 0;    
    }
    i get this wrird out put once i hit 0
    Code:
    Please enter a random nunber : 15
    Please enter a random nunber : 9
    Please enter a random nunber : 47
    Please enter a random nunber : 25
    Please enter a random nunber : 9
    Please enter a random nunber : 14
    Please enter a random nunber : 0
    -1      -1      -1      0       0       0       2       8       8       9
    9       14      15      16      25      40      47      15944   126488  2292944
    2293320 2293528 2293536 2293564 2293728 2368336 3997696 3997696 3997696 3998464
    4013640 4013649 4013680 4013696 31849508        2009145456      2009145480
    2009252574      2009252579      2009252579      2009252814      2009291924
    2009291924      2009312941      2009315348      2089872920      2089878893
    2089878896      2089879275      2089879280      HIT ENTER TO EXIT

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    No need to sort the same array 50 times.
    Code:
    for (i = 0; i < Max; i ++)
       {
           ArraySort(Num, cmpfun, Max);
            printf ("%d\t",  Num[i]);
    }
    Or print 50 items if you've only got 8.

    [edit]
    Quote Originally Posted by redmondtab
    program that will accept up to 50 validated integers from the keyboard (a null entry terminates the input process)
    Is entering a zero a "null entry"? Or could it mean entering nothing?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    104
    so that stinks. SO that means that i need to rewrite it so that the max number allowed is 50. but only print how many they entered. Ok let me think of how to code that one. Gee i'm a blond

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    Keep track of how many the user entered.

    Code:
    usersize = 0;
    for(...)
    {
       input_stuff(...);
       if(some_condition) break;
       ++usersize;
    }
    
    //now use usersize as the maximum to process data...
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 03:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM