Why %d is showing 0 in my c program output? - arrays

The program is to find the largest number amongst all the entered integers. It asks the user to enter a number 10 times or press 0 to quit, whichever is earlier. But, the output is not as expected. I will appreciate it if you can help a newbie.
#include <stdio.h>
int main()
{
int num[10];
int max, a;
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
}
for(a = 0; a < 10; a++)
{
num[0] = max;
if(max < num[a])
{
num[a] = max;
}
}
printf("This is the largest integer: %d", max); //The output is coming wrong here.
return 0;
}

Don't use num[0], you are overwriting it with max variable which is not initialized, so it is 0.
Use max to the minimum type variable (INT_MIN with limits.h header file) or initalize it to max = num[0] after input is captured.
#include <limits.h>
int max = INT_MIN;
Also you need to change your second for loop as follows so as to update the max variable as you iterate, and not the num[] variables. Loop variable starts with 1 if you already assumed max to be first element before, else loop variable will start with 0.
for(a = 1; a < 10; a++) // a=0 if max was initialized to INT_MIN above
{
if(num[a]>max)
{
max = num[a];
}
}

You never assign the max variable.
What you want to do is to check if the value entered is greater than each one you've previously entered, so you would need to use the following condition:
if (num[a] > max)
max = num[a];
You also need to initialize max to some value (let's say, if you expect to have only positive integers, it could be 0, but have a look at Jigsaw answer for a better solution): int max = 0;.
And eventually add an if-condition that checks if max is 0, that way you know if no values have been entered:
if(max == 0)
printf("No values have been entered.");
else printf("This is the largest integer: %d", max);
Notice that you can assign the elements of num and update max in the same for loop, therefore the second for becomes completely useless and you can remove it:
#include <stdio.h>
int main()
{
int num[10];
int max = 0, a;
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
if (num[a] > max)
max = num[a];
}
if(max == 0)
printf("No values have been entered.");
else printf("This is the largest integer: %d", max);
return 0;
}
I suggest you to turn on your compilers warning, especially -Wall and -Wextra, so you would notice problems like these:
<source>: In function 'main':
<source>:17:16: warning: 'max' may be used uninitialized [-Wmaybe-uninitialized]
17 | num[0] = max;
| ~~~~~~~^~~~~
<source>:6:9: note: 'max' was declared here
6 | int max, a;
| ^~~

For starters this for loop
for (a = 0; a < 10; a++)
{
printf("Enter the integer: ");
scanf("%d", &num[a]);
if (num[a] == 0)
break;
}
can enter less than 10 elements in the array num due to the condition
if (num[a] == 0)
break;
So the next loop has to traverse exactly a elements of the array not 10. So in the next loop you have to use another variable for the index as for example
for( int i = 0; i < a; i++)
The variable max was not initialized
int max, a;
So this for loop invokes undefined behavior where the variable max is assigned to elements of the array or where it is compared with elements of the array.
for(a = 0; a < 10; a++)
{
num[0] = max;
if(max < num[a])
{
num[a] = max;
}
}
Moreover the variable max is not changed within the for loop. So the loop in whole does not make sense.
Pay attention to that the user can enter 0 at once. So the array will not contain any valid values. In this case the array does not have a maximum value.
Your program can look for example the following way
#include <stdio.h>
int main( void )
{
enum { N = 10 };
int num[N];
int i = 0;
do
{
printf( "Enter the integer (0 - stop): " );
scanf( "%d", &num[i] );
} while ( num[i] != 0 && ++i < N );
int max_i = 0;
for ( int j = 1; j < i; j++ )
{
if ( num[max_i] < num[j] )
{
max_i = j;
}
}
if ( i == 0 )
{
puts( "You have not entered numbers unequal to zero." );
}
else
{
printf("This is the largest integer: %d", num[max_i] );
}
return 0;
}

You neither initialise nor ever write any value at all to the variable max. Using uninitialised variables is undefined behaviour.
You seem to know how to write 0 into a. But for max you seem to have it reversed. Remember that with = the value on the right of it goes into the variable on the left of it.
To fix the problem, turn any something = max; which occurs in your code into max = something;.

Related

C - Break while loop when typing the number 0 into a vector

I have this simple code that inserts the number entered in the terminal at the position of the vector at each loop.
int main()
{
int vector[5];
int i;
for (i = 0; i < 5; i++)
{
printf("Number: ");
scanf_s("%d", &vector[i]);
}
system("pause");
return 0;
}
My question is how I break the loop as soon as I type the number 0.
For example, imagine that my vector have 50 positions and I type 10 positions with positive integers numbers and now I want the loop ends when I enter zero and the rest of the 40 positions remain blank.
I imagine it's with the while loop, but I could not even get close to the result I want.
#define MAX_SIZE (5)
int main()
{
int vector[MAX_SIZE] = {0};
int i;
memset(vector, 0xFF, sizeof(vector)); //sets all vector[i] to -1 on init;
for (i = 0; (i < MAX_SIZE) && (i?vector[i-1]:1) ; i++) //you can add the condition to break from the for loop here. This is more complicated but its just for demonstration.
{
printf("Number: ");
scanf_s("%d", &vector[i]);
}
system("pause");
return 0;
}
The above sample code demonstrates the using the for loops conditional expression as a means to break the loop. The ternary in there prevents the i=0 case resulting in an out of bounds index, due to the way you structured your loop. The better/more readable way is below:
#define MAX_SIZE (5)
int main()
{
int vector[MAX_SIZE] = {0};
int i;
memset(vector, 0xFF, sizeof(vector)); //sets all vector[i] to -1 on init;
for (i = 0; i < MAX_SIZE; i++)
{
printf("Number: %d\n", i);
scanf_s("%d", &vector[i]);
if(vector[i] == 0)
{
break;
}
}
//system("pause");
printf("Broke the loop when i was %d\n", i);
return 0;
}
The difference between the two besides readability is that i will be incremented one extra time in the first one. Also, make sure to initialize all the variables you declare (all your vector elements are stack garbage, the 0xFF memset assigns them all to -1).

Why this for loop ends in the second index?

I wrote a program that it's duty is to read 20 numbers from user and put them in a list, after that it prints the value in array from bottom to starting point.
But program stops exactly after reading second value from input.
Source code :
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i=0;i<N;i++)
{
scanf("%i", &numbers[i]);
}
for(i=N;i<0;i--)
{
printf("%i", numbers[i]);
}
return 0;
}
I use Dev-C++ 5.6.3 as my IDE and TDM-GCC 4.8.1 as my compiler. But I don't know exactly that is this an IDE related issue or not.
If you want the loop to count downwards, then this loop
for(i=N;i<0;i--)
starts at the wrong index, and fails the test condition. It should be
for(i = N - 1; i >= 0; i--)
If you want your second loop to count down, then
for(i=N;i<0;i--)
should be
for(i=N;i>0;i--)
or the loop will not execute, as i<0 is not true to start with.
and, as #WeatherVane pointed out in the comments:
scanf("%i", numbers[i]);
should be
scanf("%i", &numbers[i]);
as you need to pass a pointer to the integer you wish to fill in with the number that scanf returns.
There is some issue with the given below for loop.
for(i=N;i<0;i--)
{
printf("%i", numbers[i]);
}
return 0;
If you Want to print the array from bottom to starting point.
You can make some changes in the for loop.
Changes :
1- Change in loop while assigning the value to i :
i = N-1 -> as the size of array is 20 and array index starts with 0.
it will go 19 to 0 to print all 20 data values.
2- changes in the condition check in for loop :
i >= 0 as we printing the reverse array.
Correct for loop should be
for(i = N-1; i >= 0; i--)
{
printf("%i", numbers[i]);
}
for(i=N-1;i>=0;i--)
For n items in an array, last index will be n-1. you need to iterate from n-1 index to 0th index.
second loop must be
for(i=N;i>=0;i--)
so index will be from 19 to 0 ( 20 number )
Try This
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i = 0; i < N; i++)
{
scanf("%i", &numbers[i]);
}
for(i = N ; i >= 0; i--)
{
printf("%i ", numbers[i]);
}
return 0;
}
Try this:
#include <stdio.h>
#define N 20
int main(void)
{
int numbers[N];
int i;
for(i = 0; i < N; i++)
{
scanf("%i", &numbers[i]);
}
for(i = N - 1; i >= 0; i--)
{
printf("%i ", numbers[i]);
}
return 0;
}
Remember that scanf always uses pointers.
Your second loop's condition, "i<0", is false to begin with. It should be "i > -1". You also need to make sure that the first time printf is called with index 19 and not 20. That is why I use pre decrement operator -- i.
for(i = N; i > -1; )
{
printf("%i\n", numbers[--i])
};

C Program - Finding the highest odd integer, stops when input a number less than 1.(Beginner)

I am currently taking programming classes this semester and at the moment we are using C language to program.
For this program it has to find the highest even integer among the values entered by the user. It stops asking values when a value less than 1 has been entered. If there is no even integer it should display "NO ODD INTEGER
at the moment this is what I have
#include<stdio.h>
#include<stdlib.h>
#define MAXNUMBERS 5
int find_maximum(int[], int);
int main()
{
int c, array[MAXNUMBERS], location, maximum, amount_numbers;
clrscr();
amount_numbers = getNumbers(array);
printf("Highest odd integer: ");
for (c = 0 ; c < MAXNUMBERS ; c++)
{
location = find_maximum(array, MAXNUMBERS);
maximum = array[location];
}
printf("%d", maximum);
getch();
}
int find_maximum(int a[], int n) {
int c, max, index;
max = a[0];
index = 0;
for(c = 1 ; c < n ; c++) {
if(a[c] > max && a[c] %1==1) {
index = c;
max = a[c];
}
}
return index;
}
int getNumbers(int arr[])
{
int c;
int n = 0;
printf("You may enter a max. %d numbers, enter a number less than 1 to end.\n", MAXNUMBERS);
for(c = 0 ; c < MAXNUMBERS ; c++)
{
scanf("%d", &arr[c]);
fflush(stdin);
n++;
if(arr[c] < 1)
{
break;
}
}
return n;
}
*Problem has been fixed question can be closed or deleted because of unecessary question
I suggest this approach :
int main()
{
int c, array[MAXNUMBERS], maximum, amount_numbers;
clrscr();
getNumbers(array);
location = find_maximum(array, MAXNUMBERS);
if (location == -1 )
printf("No Even integer");
else {
maximum = array[location];
printf("Highest even integer :%d", maximum);
}
getch();
}
int find_maximum(int a[], int n) {
int c, max, index;
max = -10000; // A very low number
index = -1; // shows that no even integer is found yet.
for(c = 1 ; c < n ; c++) {
if(a[c] > max && a[c] %2 == 0) {
index = c;
max = a[c];
}
}
return index;
}
int getNumbers(int arr[])
{
int c;
printf("You may enter a max. %d numbers, enter a number less than 1 to end.\n", MAXNUMBERS);
for(c = 0 ; c < MAXNUMBERS ; c++)
{
scanf("%d", &arr[c]);
fflush(stdin);
if(arr[c] < 1)
{
break;
}
}
}
You should first scan the array from the user which is what getNumbers function does.
Next you need to find the highest even integer which is what find_maximum funciton does.
this function returns index of the highest event integer in your array. the default value is -1 which indicates that no even value is found ! if it's given any other value means that found the highest even integer.
so now that you have the index you check if it's -1(not found) or it's found (returned index).
There is no sense to define an array for a sequence of numbers with undefined size that to determine the maximum value. When a next number is entered you can compare it with the current maximum value provided that the entered number is even.
The program can look the following way
#include <stdio.h>
int main( void )
{
int max = 0;
int value;
printf("Enter a sequence of integer numbers.\n"
"Enter a number less than 1 to end: ");
while (scanf("%d", &value) == 1 && !(value < 1))
{
if (value % 2 == 0 && max < value) max = value;
}
if (max != 0)
{
printf("Highest even integer: %d\n", max);
}
else
{
puts("No even integer");
}
}
As for your program then this loop is wrong.
for (c = 0 ; c < MAXNUMBERS ; c++)
{
location = find_maximum(array, MAXNUMBERS);
maximum = array[location];
}
For starters the number of entered values can be less than MAXNUMBERS. You have to use at least the variable amount_numbers in the condition.
Moreover the loop is not needed Because it does the same that is it calls several times the function find_maximum and nothing is changed with the function result between iterations of the loop. Moreover you have to check whether the variable location gets value 0 and whether the element with this index is less or greater than 1. Otherwise you can show invalid result.
As commented, you are only required to output the answer so there is no need to store the input in an array. This shows how to do it with a simple loop. The program also ends at non-numeric input (effectively a 0 value).
#include <stdio.h>
int main(void) {
int innum = 0;
int maximum = 0;
while(scanf("%d", &innum) == 1 && innum > 0) {
if(innum % 2 == 0 && maximum < innum) {
maximum = innum;
}
}
if(maximum < 2) {
puts("NO EVEN INTEGER");
}
else {
printf("Highest even integer: %d\n", maximum);
}
return 0;
}
Program session:
3 8 1 5 3 4 -5
Highest even integer: 8

How to show the digits which were repeated in c?

The question is that show the digits which were repeated in C.
So I wrote this:
#include<stdio.h>
#include<stdbool.h>
int main(void){
bool number[10] = { false };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] == true)
{
printf("%d ", digit);
}
number[digit] = true;
n /= 10;
}
return 0;
}
But it will show the repeated digits again and again
(ex. input: 55544 output: 455)
I revised it:
#include<stdio.h>
int main(void){
int number[10] = { 0 };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] == 1)
{
printf("%d ", digit);
number[digit] = 2;
}
else if (number[digit] == 2)
break;
else number[digit] = 1;
n /= 10;
}
return 0;
}
It works!
However, I want to know how to do if I need to use boolean (true false), or some more efficient way?
To make your first version work, you'll need to keep track of two things:
Have you already seen this digit? (To detect duplicates)
Have you already printed it out? (To only output duplicates once)
So something like:
bool seen[10] = { false };
bool output[10] = { false };
// [...]
digit = ...;
if (seen[digit]) {
if (output[digit])) {
// duplicate, but we already printed it
} else {
// need to print it and set output to true
}
} else {
// set seen to true
}
(Once you've got that working, you can simplify the ifs. Only one is needed if you combine the two tests.)
Your second version is nearly there, but too complex. All you need to do is:
Add one to the counter for that digit every time you see it
Print the number only if the counter is exactly two.
digit = ...;
counter[digit]++;
if (counter[digit] == 2) {
// this is the second time we see this digit
// so print it out
}
n = ...;
Side benefit is that you get the count for each digit at the end.
Your second version code is not correct. You should yourself figured it out where are you wrong. You can try the below code to print the repeated elements.
#include<stdio.h>
int main(void){
int number[10] = { 0 };
int digit;
long n;
printf("Enter a number: ");
scanf("%ld", &n);
printf("Repeated digit(s): ");
while (n > 0)
{
digit = n % 10;
if (number[digit] > 0)
{
number[digit]++;;
}
else if (number[digit] ==0 )
number[digit] = 1;
n /= 10;
}
int i=0;
for(;i<10; i++){
if(number[i]>0)
printf("%d ", i);
}
return 0;
}
In case you want to print the repeated element using bool array (first version) then it will print the elements number of times elements occur-1 times and in reverse order because you are detaching the digits from the end of number , as you are seeing in your first version code output. In case you want to print only once then you have to use int array as in above code.
It is probably much easier to handle all the input as strings:
#include <stdio.h>
#include <string.h>
int main (void) {
char str[256] = { 0 }; /* string to read */
char rep[256] = { 0 }; /* string to hold repeated digits */
int ri = 0; /* repeated digit index */
char *p = str; /* pointer to use with str */
printf ("\nEnter a number: ");
scanf ("%[^\n]s", str);
while (*p) /* for every character in string */
{
if (*(p + 1) && strchr (p + 1, *p)) /* test if remaining chars match */
if (!strchr(rep, *p)) /* test if already marked as dup */
rep[ri++] = *p; /* if not add it to string */
p++; /* increment pointer to next char */
}
printf ("\n Repeated digit(s): %s\n\n", rep);
return 0;
}
Note: you can also add a further test to limit to digits only with if (*p >= '0' && *p <= '9')
output:
$./bin/dupdigits
Enter a number: 1112223334566
Repeated digit(s): 1236
Error is here
if (number[digit] == true)
should be
if (number[digit] == false)
Eclipse + CDT plugin + stepping debug - help you next time
As everyone has given the solution: You can achieve this using the counting sort see here. Time complexity of solution will be O(n) and space complexity will be O(n+k) where k is the range in number.
However you can achieve the same by taking the XOR operation of each element with other and in case you got a XOR b as zero then its means the repeated number. But, the time complexity will be: O(n^2).
#include <stdio.h>
#define SIZE 10
main()
{
int num[SIZE] = {2,1,5,4,7,1,4,2,8,0};
int i=0, j=0;
for (i=0; i< SIZE; i++ ){
for (j=i+1; j< SIZE; j++){
if((num[i]^num[j]) == 0){
printf("Repeated element: %d\n", num[i]);
break;
}
}
}
}

How to find the largest and smallest number in an array in c

I have to find a way to display the Maximum and Minium number in an array, the size of the array is 100 and will not exceed that and there is not need for input validation. The program will keep asking for input until 0 is encountered and it too will get added to the array.
I have everything figured out except how to keep track which is the largest and smallest value. I'd appreciate it if someone can fix my code or show me.Another problem I'm having is getting the loop to terminate and do max/min calculation within the while loop when the input is equal to 0.
/*
============================================================================
Name : test.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#define n 100
int main(void){
int numbers[n];
int i = 1;
int j;
int input;
int maxvalue;
int minvalue;
printf("Enter the next array element>");
input = scanf("%d", &numbers[100]);
while (input != 0){
numbers[i] = input;
i++;
printf("Enter the next array element, while loop>");
input = scanf("%d", &numbers[n]);
if (input == 0){
printf("Enter the next array element, if loop");
numbers[i] = 0;
for (j =2;j <= i; j++){
minvalue = numbers[1];
j++;
if (numbers[j] > minvalue){
maxvalue = numbers[j] ;
}
else{
minvalue = numbers[j] ;
}
}
}
}
printf("%f\t", maxvalue);
printf("%f\n", minvalue);
}
EDIT: I took all off your suggestions and edited my code. This is my code below. However, it's output isnt what I'm expecting.
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(void){
int numbers[N];
int i = 0;
int j;
int input;
int maxvalue;
int minvalue;
printf("Enter the next array element>");
scanf("%d", &input);
while (input != 0){
numbers[i] = input;
i++;
if (input == 0){
i++;
numbers[i] = 0;
minvalue = numbers[0];
maxvalue = numbers[0];
for (j=0;j<=i-1;j++){
if (minvalue >= numbers[j]){
minvalue = numbers[j];
}else if (maxvalue <= numbers[j]){
maxvalue = numbers[j];
}
}
/* min = value of first array element
max = value of first array element
begin loop for each array element, index = 0 to (n-1)
--- if array element value is less than min, set min to this value
--- if array element value is more than max, set max to this value
increment index and repeat loop til last index is completed
average = sum / number of elements (n).
max and min will hold their correct values.*/
}
printf("Enter the next array element, while loop>");
scanf("%d", &input);
}
printf("%d\t", maxvalue);
printf("%d", minvalue);
}
This is the output, I'm getting! Can someone solve this for me.
Enter the next array element>1
Enter the next array element, while loop>2
Enter the next array element, while loop>3
Enter the next array element, while loop>0
12190144 l6Press [Enter] to close the terminal
FINAL EDIT: I SOLVED THIS ON MY OWN. I put the min/max checking outside the master WHILE loop, this allowed the input of 0 to be entered in the array.
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(void){
int numbers[N];
int i = 0;
int j;
int input;
int maxvalue =1;
int minvalue = 1;
printf("Enter the next array element>");
scanf("%d", &input);
minvalue = input;
maxvalue = input;
while (input != 0){
numbers[i] = input;
++i;
printf("Enter the next array element>");
scanf("%d", &input);
if (input == 0){
numbers[i] = 0;
++i;
}
}
for (j =0;j<i;j++){
if (numbers[j] >= maxvalue){
maxvalue = numbers[j];
}
if(numbers[j] < minvalue){
minvalue = numbers[j];
}
}
printf("%d\t", maxvalue);
printf("%d\n", minvalue);
}
First of all, you're assigning input to the return value of scanf(). This is the number of items assigned by the call, and since you say the input will always be correct, this value will always be 1.
Secondly, you're writing past the end of the numbers[] array with the line:
input = scanf("%d", &numbers[100]);
(you should do scanf("%d, &input) instead, and assign numbers[i] to input in your loop.
Finally, you don't need to recalculate maxvalue and minvalue by iterating through numbers[] every iteration of your loop. Instead, just compare them to input and assign them accordingly.
Hopefully this puts you on the right track.
It looks like your central problem is that you compare each number only against minvalue. That's fine for deciding whether to replace the current minvalue, but obviously it doesn't tell you anything about the relationship of each element to maxvalue.
Another problem: it makes sense to initialize minvalue from the first element, but not if you do it in the loop. That just invalidates all your prior work.
You need to do the same initialization with maxvalue as well. You should initialize that number to the first value.
You should also make a decision about calculating the min and max as you accumulate the data or in a pass through the data when done. What you don't want to do, however, is loop through past elements with every new one. That gives your program quadratic time complexity for no benefit.
Finally, don't tolerate crummy formatting. Debugging always involves studying the code and you will want it to always be perfectly formatted both to be professional about things and also to facilitate reading your own work.
You are asking two questions, about the strategy for the min / max computation and for the loop. Don't do that (to yourself) but solve one problem at a time. So first put something like
signed int input[] = { 8, -5 , /* some more values */ };
size_t const n = sizeof input/ sizeof input[0];
at the start and forget about your scanf problems.
Then wrap your min/max detection in the appropriate loop instruction.
Then compile your code with warnings on: e.g -Wall for gcc, but this might vary for your compiler.
Mine the tells me something:
test-numbers.c:21: warning: 'maxvalue'
may be used uninitialized in this
function test-numbers.c:22: warning:
'minvalue' may be used uninitialized
in this function
This tells you that you are doing something very wrong in not considering the starting point of your algorithm well.
I've reindented your code and replaced lots of it with `/* ...PLACEHOLDER... */
#include <stdio.h>
#include <stdlib.h>
#define N 100
int main(void) {
int numbers[N];
int i = 0;
int input;
int maxvalue;
int minvalue;
printf("Enter the next array element>");
scanf("%d", &input);
while (input != 0) {
numbers[i] = input;
i++;
if (input == 0) {
/* ...PLACEHOLDER... */
}
printf("Enter the next array element, while loop>");
scanf("%d", &input);
}
printf("%d\t", maxvalue);
printf("%d", minvalue);
}
Hopefully you can see what happens when you enter 1, or 2, or 3 and when you enetr 0.
Hint: maxvalue and minvalue values are never changed.
Another hint: how many times does the while() line execute?
Edit with example run
For this example run, code is on the left side, what happens is on the left side
printf("Enter the next array element>"); |
scanf("%d", &input); | Enter 42
|
while (input != 0) { | input is 42, so you do the loop
numbers[i] = input; | numbers[0] = 42
i++; | i = 1
|
if (input == 0) { | input != 0; skip placeholder
/* ...PLACEHOLDER... */ |
} |
printf("Enter the next ...>"); |
scanf("%d", &input); | enter 3
} |
while (input != 0) { | input is 3
numbers[i] = input; | numbers[1] = 3
i++; | i = 2
|
if (input == 0) { | input != 0; skip placeholder
/* ...PLACEHOLDER... */ |
} |
printf("Enter the next ...>"); |
scanf("%d", &input); | enter 0
} |
while (input != 0) { | input is 0, skip while body
/* ...PLACEHOLDER... */ |
} |
printf("%d\t", maxvalue); | maxvalue hasn't been initialized
printf("%d", minvalue); | minvalue hasn't been changed
int cmp(const void *a,const void *b)
{
return *(const int*)a-*(const int*)b;
}
...
qsort( numbers, 100, sizeof(numbers[0]), cmp );
printf("\nmin: %d\nmax: %d",numbers[0],numbers[99]);

Resources