2 largest elements; -1 to stop loop

This is a discussion on 2 largest elements; -1 to stop loop within the C Programming forums, part of the General Programming Boards category; Here's most of my program. program is create a random generated array of 50 elements elements are values 1-500 read_array ...

  1. #1
    Peachy
    Guest

    2 largest elements; -1 to stop loop

    Here's most of my program.

    program is create a random generated array of 50 elements
    elements are values 1-500

    read_array function initializes new array of integers each time

    print_array function prints current array after stopping program with entry of -1 from keyboard /// have no idea how to code this

    Two_largest function takes array from read_array function and finds the two largest element values

    finally the main() function is to print these values

    (1) i do not know if the two largest values are to be printed for each and every array created before -1 stops program
    and if so, how could you print out say
    (2) what kind of code is used to scan in a -1 to stop program
    -- my friend says that control-Z is used commonly, and
    there is an ASCII code for that -- what is this???
    (3) the whole program is to be one DO/WHILE Loop, I didn't know where to put that in the main() ; but assume it has something to do with the -1 stoppage component
    (4) what are the int values for each function doing??? (the no-array and non-pointer int values is what I refer to here.)


    Code:
       /*   Two largest integers  */
       /*   stop when -1 entered  */
     
     
       #include <stdio.h>
       #include <stdlib.h>
       #include <time.h>
       
       #define NUM_ELEM 50
       #define RANDOM_NUM 500
       
       void Two_largest(int array2[], int m, int *lg, int *lg2);
       void read_array(int array1[], int r);
       void print_array(int array3[], int p);
       
       srand (time(NULL));
       
       main()
       {
          int a, b, c;
          int read[NUM_ELEM]={0};
          int print[NUM_ELEM]={0};
          int sortedarray[NUM_ELEM]={0};
          int *largest;
          int *secondlargest;
       
          read_array(read,a);
          print_array(print,b);
          Two_largest(sortedarray, c, *largest, *secondlargest);
       
          printf("%d  %d\n", &largest, &secondlargest);
       
         }
       
       read_array(int array1[], int r)
       
       {
       
          int i;
       
          for (i=0; i<NUM_ELEM; i++)
       
              {
                 array1[i]=rand()%RANDOM_NUM+1;
              }
        
        }
      
       print_array(int array3[], int p)
      
       {
           int i;
      
           for (i=0; i<NUM_ELEM; i++)
       void Two_largest(int array2[], int m, int *lg, int *lg2)
        {
          int i,j;
       
          for (i=o; i<NUM_ELEM; i++)
       
            {
               for (j=i+1; j<NUM_ELEM; j++)
       
               {
                   temp = array2[i];
                   array2[i] = array2[j];
                   array2[j] = temp;
                  }
               }
        
               *lg = array2[j];
               *lg2 = array2[j-1];
      }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > (1) i do not know if the two largest values are to be printed for each and every array created
    I'd say yes they are.
    But if the requirement is vague, go back to whoever set the problem to clarify.

    > (2) what kind of code is used to scan in a -1 to stop program
    Hard to say - EOF is commonly assigned the value of -1, so pressing ctrl-z to exit the program might be what is needed.
    If this is the case, the code for main would look like this.

    Code:
    int array[NUM];
    int large1, large2;
    int ch;
    while ( (ch=getchar()) != EOF ) {
        read_array( array );
        print_array( array );
        two_largest( array, &large1, &large2 );    // compare with your use of * and &
        printf( "Largest %d %d\n", large1, large2 );
        printf( "Press enter for another go, ctrl-z to exit\n" );
    }
    > (3) the whole program is to be one DO/WHILE Loop,
    Remarkable.....

    > (4) what are the int values for each function doing?
    I would imagine that would be the array length, if these routines were to work with any sized array.

    So a call would be
    Code:
    read_array ( array, NUM );
    And the function would be
    Code:
    void read_array(int array1[], int r) {
        for ( i = 0 ; i < r ; i++ ) {
            array1[i]=rand()%RANDOM_NUM+1;
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Peachy
    Guest

    Exclamation more clarification

    I need to clarify:

    the following prototypes of the functions used are required, with parameters as shown:


    void Two_largest(int[], int , int *, int *);
    void read_array(int [], int );
    void print_array(int [], int );

    My question was about the 'int' variables; not the arrays or pointers.

    program from main calls the read_array first, initializes elements from random num generator, I am assuming passing the array back to main;

    then main calls print_array func and prints the current array; then passes array again back to main

    then main calls Two_largest function and then the fun begins, sorting and finding the two_largest element values of original array

    My question again, is what are the parameters of the single int variable (not the int array or int pointer) for in each function called??? Does this have to do with the user input of -1 to stop program????

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    > My question was about the 'int' variables; not the arrays or pointers.
    Already answered.

    > I am assuming passing the array back to main;
    The array doesn't move anywhere, it stays in main. When you pass an array to a function, you're really just passing a pointer to the array. So any changes you make to the array within a function directly update the array within main.
    Look at my code - there is only one array, and each function gets to look at / modify it in turn.

    > sorting and finding the two_largest element values of original array
    - sorting was not in the original requirement
    - you don't need to sort the array in order to find the two largest
    - you don't print the sorted array, so there's no point to sorting

    > My question again, is what are the parameters of the single int variable
    It's the number of elements in the array - how many times do I have to tell you?

    In your original code, it would be
    read_array(read,NUM_ELEM);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Peachy
    Guest

    where do I put code

    Salem:

    A-ha I get it now (re: int variable = number elements in array).

    Where would I put the code for the EOF to stop program???










    Peachy

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using realloc for a dynamically growing array
    By broli86 in forum C Programming
    Replies: 10
    Last Post: 06-27-2008, 05:37 AM
  2. need help, fgets won't stop while loop
    By Enkid in forum C Programming
    Replies: 6
    Last Post: 10-26-2007, 07:15 AM
  3. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  4. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  5. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM

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