Good evening folks. I was just assigned a task by my professor. I'm starting to find it becoming harder and harder as my professor isn't improving his teaching skills and he prefers to tell lame jokes about faulty programming rather than teach properly. not to mention his heavy accent makes it harder :/ . We've just got into arrays and I don't even understand how to do functions. (we are not covering string yet).
heres the question I received.
Programming Problems on Arrays
1. Write a C program that asks the user for many integers until a ZERO is
entered. All the integers except ZERO will be stored in an array. The
program will print out the smallest integer, the largest integer and
a table of all the integers (except ZERO). The program must make sure
that the user will not enter more than 50 integers (including ZERO).
Here is a sample ouptut:
smallest integer: -20
largest integer: 99
11
98
3
-1
-4
-20
35
67
99
75
*I have completed question 1*
2. Redo Question 1 by building two functions and one main program.
The main program should produe the same sample output as in Question 1.
/* This function will get no more than 50 integers from the user.
It will stop asking the user for another integer if stopper is entered.
All integers (except stopper) are stored in the array (num).
The function returns the total number of integers that are stored in
the array. */
Code:
int get_numbers( int num[ ], int stopper );
/* This function finds the smallest and the largest integers in
the array (z). size is the total number of integers stored in
the array. */
Code:
void find( int z[ ], int size, int * min, int * max );
i've already completed part one with a little help from a tutor who is only available 1 hour a week and if you manage to even get in the time slot.
Here is my code for question 1:
Code:
/*Quiz Submitted by Umeed Malik: Student Number 32109076*/
#include <stdio.h>
#define SIZE 50
#define INVALID 0
main () {
int Nums[SIZE];
int Smallest=0, Largest=0, entry, counter=-1, i;
do{
printf("Please enter an integer(enter 0(ZERO) to exit): ");
scanf("%d", &entry);
counter += 1;
if (entry != INVALID){
if (entry > Largest){
Largest = entry;
Nums[counter] = entry;
} else if (entry < Smallest) {
Smallest = entry;
Nums[counter] = entry;
} else {
Nums[counter] = entry;
}
}
}while (entry != INVALID && counter != SIZE);
printf("\n\nSmallest integer: %d", Smallest);
printf("\nLargest integer: %d\n", Largest);
for (i=0; i<counter; i++){
printf("%d\n", Nums[i]);
}
thanks to you guys for explaining the importance of clean and well formatted coding I've begun to understand what I'm doing more lol. However I have no clue as how to add a function to this array.
The only thing i really understand about functions is it helps to avoid writing the same code over and over.
I do appreciate the help, thanks in advance.