array

This is a discussion on array within the C Programming forums, part of the General Programming Boards category; hello, i have an integer array and i want to know wich is the bigger number in that array example: ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    4

    array

    hello, i have an integer array and i want to know wich is the bigger number in that array

    example:

    | 1 | 2 | 6 | 3 | 5 | 7 | -> the bigger number on the array is 7.

    how do i make this??

    thanks

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    wow....... talk about lazy....


    use a loop and a simple algorithm
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how do i make this??
    How do you test two numbers and save the larger of them? Save the first number in the array as the largest, then walk across the rest of the array with the largest as one of the two numbers to test.
    My best code is written with the delete key.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    This is a very tough question and should be handled with much care as incorrect code could easily cause some sort of motherboard overheating.

    Example:
    Code:
    struct array_number {
      int number;
      struct array_number *next;
    };
    
    int main(void) {
      int array[12] = {0, 1, 2 ,3 ,4 5, 7, 8, 9, 10, 11}, i, size;
      struct number_array *base, *p;
    
      p = base = malloc(sizeof(struct number_array);
      
      if(base == NULL)
        return EXIT_FAILURE;
    
      base->next = NULL;
    
      for(i = 0, size = sizeof(array)/sizeof(int); i < size; i++) {
        p->number = array[i];
        p->next = malloc(sizeof(struct number_array));
    
        if(p->next == NULL)
          if(base->next)
            for(p = base->next; p != NULL; base = p, p = p->next)
              free(base);
          else
            free(base);
    
        p = p->next;
      }
    
      for(p = base; p != NULL; p = p->next) {
        printf("%d ", p->number);
      }
    
      if(base->next)
        for(p = base->next; p != NULL; base = p, p = p->next)
          free(base);
      else
        free(base);
    
      puts("\nwhich number is largest?");
      scanf("%d", &i);
      
      printf("\n\nafter lengthy computation, this program found %d to be the largest number in "
        "the array", i);
    
      return EXIT_SUCCESS;
    }

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by master5001
    This is a very tough question and should be handled with much care as incorrect code could easily cause some sort of motherboard overheating.
    Was that supposed to be an example of incorrect code or just complete overkill?
    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.*

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Was that supposed to be an example of incorrect code or just complete overkill?
    Incorrect code, of course. It's only overkill if you can get far enough to run it.
    My best code is written with the delete key.

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Its a little of both. I figured if you played with the code, by the time you get it running you will have realized "hmm maybe I should have tried doing my simple code by myself."

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, 02: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

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