sorry if this post looks crappy, this is my first try. ok, I'm having trouble with an assignment in my programming class. here are the assignment instructions:
"The purpose of this program is to find the two largest elements
of each of several integer arrays containing 50 elements. The program will use
functions with the prototypes:
void two_largest(int[], int, int*, int*);
void read_array(int[], int);
void print_array(int[], int);
The program is supposed to be just one do/while loop, which will be repeated
until a number -1 is entered from a keyboard. On every step of this loop:
1) use the function read_array to initialize a new array of integers
(this function will initialize array elements to random numbers from 1 to 500
with the help of functions rand() and srand(time(NULL)));
2) use the function print_array to display the current array;
3) call the function two_largest to find the two largest numbers in the array;
4) display these numbers in main."
I have the program pretty much finished except for two problems. I don't understand how to terminate the loop by entering -1 and I'm not exactly sure how to make the rand() and srand() loop I have into a do/while loop because we haven't covered the two functions in class. we were only given example programs to figure out how these two functions work. Here is my code for what I have:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 50
void two_largest(int [], int, int *, int *);
void read_array(int [], int);
void print_array(int [], int);
int main ()
{
int a[N], b, c, d, e;
read_array(a, b);
print_array(a, b);
two_largest(a, b, &c, &d);
printf("The largest value is: %d\n", c);
printf("The second largest value is: %d\n", d);
return 0;
}
void read_array(int a[], int b)
{
srand(time(NULL));
for(b=0; b<N; b++)
a[b] = rand() % 500;
}
void print_array(int a[], int b)
{
for(b=0; b<N; b++)
printf("%3d%c", a[b], b % 10 == 9 ? '\n' : ' ');
}
void two_largest(int a[], int b, int *x, int *y)
{
int i;
*y=a[0];
*x=a[0];
for(b=0; b<N; b++)
{ if (a[b] > *x)
*x = a[b];
}
for(i=0; i<N; i++)
{ if (a[i] > *y && a[i] < *x)
*y=a[i]; }
}
just wondering if anyone had some suggestions? I'm not very good with this stuff since this is a beginning level class so please try to keep it simple.