Keyboard input of array of bits - c

I want to make a program that applies some logic gates (AND, OR, XOR) to elements of two arrays of 1 and 0. But I am having problems with the user input of these arrays. I don't know how to make the arrays store only 1 and 0, for example if I type 5 I want the program to tell me it's neither 0 nor 1 and start over, I tried something but it's not working:
int v1[50],v2[50],i,j,n;
printf("Number of elements in arrays : ");
scanf("%d",&n);
printf("Introduce elements of first array :\n");
for(i=0;i<n;i++)
if(v1[i] == 0 || v1[i]==1)
scanf("%d",&v1[i]);
else (i'll make it a function and I want it to repeat if the elements given are not 1 and 0)
for(i=0;i<n;i++)
printf("%d",v1[i]);

In your first for loop, where you are reading the input, you should read the input first, and then decide whether you want to have the user try the input again. So, the first few lines of your for loop should look like this:
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
if (!(v1[i] == 0 || v1[i] == 1)) {
printf("Invalid input, please try again");
//Ask for another input, but do not advance i
}
}
This code will tell the user if they inputted a bad character, but it will not update the array correctly. To do this, all you need to do is decrement i once. This will make the previous "bad" value in v1 get overwritten.
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
if (!(v1[i] == 0 || v1[i] == 1)) {
printf("Invalid input, please try again");
i--;
}
}
We are not done, however. In your original code, you defined v1 to be an array of 50 elements. What if someone wants to input 51 elements? You would eventually end up with accessing an array index that is out of bounds, which could lead to some very big issues. So, you need to do some dynamic memory allocation using malloc
int *v1, i, n;
printf("How many elements will be in the bit array? ");
scanf("%d", &n);
//Dynamically allocate enough memory for an integer array of length n
v1 = (int *) malloc(n * sizeof(int));
You can read more about malloc here.
So, the whole code would look like this:
#include <stdlib.h>
#include <stdio.h>
int main() {
int *v1, i, n;
printf("How many elements will be in the bit array? ");
scanf("%d", &n);
//Dynamically allocate enough memory for an integer array of length n
v1 = (int *) malloc(n * sizeof(int));
printf("Input the elements of the first array (separated by newlines):\n");
for (i = 0; i < n; i++) {
scanf("%d", &v1[i]);
if (!(v1[i] == 0 || v1[i] == 1)) {
printf("Invalid input, please try again");
i--;
}
}

Suppose you have an array consisting of 50 elements:
int v1[50];
If you want to fill it with values only of 0 and 1 you should set up a while loop, until the user puts in correct data:
int iter, result;
for (iter = 0; iter < 50; iter++)
{
while ((result = scanf("%d", &v1[iter])) != 1 // no number was found
|| (v1[iter] != 0 && v1[iter] != 1)) // OR it was and it wasn't 0 or 1
{
if (result != 1)
scanf("%*s"); // case 1: dispose of bad input
else
printf("Please, use only values 0 or 1\n"); // case 2: remind the user
}
}
}

Related

Pythagorean triplets program

I have written a program that should be rather simple but on execution, it is not giving the wanted results. Even when debugging the program, I guess I found the error (getting stuck in the first if condition) but I'm not able to solve it (my inexperience perhaps). Anyways, this program, which should have been frugal, took 3 days whereas I expected it to take mere hours. Please help me with guiding me where I'm going wrong and how to solve it.
Here is the code
/*WAP to read pre entered no. of ints. consider only +ve and print the pythagorean triplets in them.*/
#include <stdio.h>
int main(){
int c,p,pp,count=0,a;
printf("How many entries to accept?\n");
scanf("%d",&a);
printf("Enter the nos.\n");
for (int i = 0; i < a; i++)
{
scanf("%d",&c);
if (c<0) //skip -ve nos.
{
continue;
}
if (count==0)
{
pp=c;
count++;
}
else if (count==1)
{
p=c;
count++;
}
else if ((pp*pp)+(p*p)==(c*c)) //Tracking count not necesarry after first three
{
printf("Pythagorean triplet found\n");
printf("%d %d %d",pp,p,c);
pp=p;
p=c;
}
}
return 0;
}
The main objective is to first scan a no. to signify the inputs to be read. Then scan the inputs, separated by a space or enter, in a loop which will only accept the no. of inputs stated before. It should neglect any -ve entries. It should print out the Pythagorean triplet if it encounters one, in a consecutive manner i.e. the triplet should appear one after the other & not randomly. We have to do the task without using arrays.
sample input is (you can consider any)(all given through the terminal)
(no. of entries)
6
1 -1 3 4 -4 5
(Here it will ignore -1 & -4)
expected output will be
Pythagorean triplet found
3 4 5
I am still learning so sorry for the elaborate program.
Thank you in advance.
since I cant see the input file I dont know if the values are sorted, since we need to identify which is the hypotenuse, makes it a bit more fiddly.
Also not clear what 'skip negatives' means. Does it mean
that we might see 3 -6 4 5 and say 'yes 3,4,5' is a triple
that we might see 3 -4 5 and say yes 3 4 5
or that we might see 3 -4 5 and simply ignore the whole set
I have assumed the first one
#include <stdio.h>
int main() {
printf("How many entries to accept?\n");
int a;
if (scanf("%d", &a) != 1) {
printf("bad input\n");
return (-1);
}
printf("Enter the nos.\n");
for (int i = 0; i < a; i++)
{
int sides[3] = { 0 };
int max = 0; // longest side length -> hypot
for (int j = 0; j < 3; j++)
{
int c;
if (scanf("%d", &c) != 1) {
printf("bad input\n");
return (-1);
}
if (c < 0) //skip -ve nos.
j--; // try again
else {
if (c > max) {
max = c;
}
sides[j] = c;
}
}
int hyp = max * max; // hypotenuse squared
int adjTot = 0; // adj sides squared total
for (int j = 0; j < 3; j++)
{
if (sides[j] == max)
continue;
adjTot += sides[j] * sides[j];
}
if (adjTot == hyp)
printf("%d %d %d is py\n", sides[0], sides[1], sides[2]);
else
printf("%d %d %d isnt py\n", sides[0], sides[1], sides[2]);
}
return 0;
}
Since you say you are reading from a file it just exits if there is non numeric data

how can I define limit of an array by the quantity of numbers put in scanf?

Example: input: 420 50 -4
output: Numbers 3
Positive 2
Negative 1
and also for the same code:
input: 420 50 -4 7
output: Numbers 4
Positive 3
Negative 1
#include<stdio.h>
#define N 2
int main()
{
int a[N], i=0, n=0, k=0, z=0;
for(i=0; i<N; i++)
{
scanf("%d" , &a[i]);
if((a[i] >= -10000 && a[i] <= 10000 ))
n++;
if(a[i]>0)
k++;
if(a[i]<0)
z++;
}
printf("Numbers:%d \n", n);
printf("Positive:%d \n", k);
printf("Negative:%d \n", z);
return 0;
}
new issue
So the idea is this, I need my programm(mostly done by yano here) to only be able to load numbers ranging for -10000 to 10000 including border numbers, if other numbers would be loaded, the program should print the correct numbers, and ignore the incorrect (more like remove from array and replacing the element with the rest, which is correct, whilst reducing the total number of elements in the array)
example
input 140 -154161 20 30
output 140, 20, 30
Error: Input is outside interval!"
#include <stdio.h>
#include <stdlib.h>
#define INITIAL_SIZE 10
void
printArray (const int *myArray, size_t numsEntered)
{
int i, c = 0, k = 0, z = 0, s=0, l=0, sum=0, max, min;
float pk, pz, ps, pl, prumer;
for (size_t i = 0; i < numsEntered; i++) //does math
{
sum = sum + myArray[i];
if (i)
printf (", ");
printf ("%i", myArray[i]);
if ((myArray[i] >= -10000 && myArray[i] <= 10000))
c++;
if (myArray[i] > 0)
k++;
if (myArray[i] < 0)
z++;
if(myArray[i]%2==0)
s++;
else
l++;
max = myArray[0];
min = myArray[0];
if(myArray[i] > max)
{
max = myArray[i];
}
if(myArray[i] < min)
{
min = myArray[i];
}
}
if ((myArray[i] >= -10000 && myArray[i] <= 10000)) //checks if arrays are in range
{
prumer=(float) sum/2;
pk = (float) k / c;
pz = (float) z / c;
ps = (float) s / c;
pl = (float) l / c;
printf ("\n");
printf ("Pocet cisel: %d\n", c);
printf ("Pocet kladnych: %d\n", k);
printf ("Pocet zapornych: %d\n", z);
printf ("Procento kladnych: %.2lf\n", pk);
printf ("Procento zapronych: %.2lf\n", pz);
printf("Pocet sudych: %d\n", s);
printf("Pocet lichych: %d\n", l);
printf ("Procento sudych: %.2lf\n", ps);
printf ("Procento lichych: %.2lf\n", pl);
printf("Prumer: %.2lf\n", prumer );
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
}
if (myArray[0]<-10000 || myArray[0]>10000) //checks if first element is in wrong range
programm prints arror and returns 0
{
printf("\n");
printf ("Error: Vstup je mimo interval!");
}
}
int
main ()
{
int lastArray = 0, end = 0, b = 0, i=0;
size_t arraySize = INITIAL_SIZE;
size_t numsEnteredSoFar = 0;
int *myArray = malloc (sizeof (*myArray) * arraySize);// initially make room for 10
if (myArray == NULL)
exit (-1);
while (1)
{
int curEntry, size = sizeof (myArray) / sizeof (int);
char ch;
if (scanf ("%d", &curEntry) == 1)
{
b = curEntry;
end = numsEnteredSoFar;
ch = fgetc (stdin);
myArray[numsEnteredSoFar++] = curEntry;
if (numsEnteredSoFar == arraySize)
{
arraySize += INITIAL_SIZE;
int *temp = realloc (myArray, arraySize * sizeof (*myArray));
if (temp == NULL)
{
fprintf (stderr, "out of memory\n");
exit (-1);
}
else
{
myArray = temp;
}
}
}
for (size_t i = 0; i < numsEnteredSoFar; i++)
if((myArray[i]<-10000 || myArray[i]>10000)) //checks if input is in range if not exits
{
if (i) //my attempt for making this work
printf (", ");
printf ("%i", myArray[i]);
printf ("\n");
printf ("Error: Vstup je mimo interval!");
exit (-1);
}
if (ch == 10)
{
break;
}
}
printArray (myArray, numsEnteredSoFar);
free (myArray);
return 0;
}
There are several ways to solve this problem:
Declare an array that's large enough to accommodate the largest conceivable size of your data.
Include a size at the beginning of your data, and use that to malloc your array.
Use a data structure that doesn't depend on a fixed size, such as a linked list.
This example allows the user to enter numbers "indefinitely" without the need for prompting how many to enter. Of course, your computer only has so much RAM, so there is a limit, but not a practical limit. Essentially, you need to choose an initial size, then allocate more space dynamically when that size is reached.
#include <stdio.h>
#include <stdlib.h>
#define INITIAL_SIZE 10
void printArray(const int* myArray, size_t numsEntered)
{
for (size_t i=0; i<numsEntered; i++)
{
printf("myArray[%zu] = %d\n", i, myArray[i]);
}
}
int main(void)
{
size_t arraySize = INITIAL_SIZE;
size_t numsEnteredSoFar = 0;
int* myArray = malloc(sizeof(*myArray) * arraySize); // initially make room for 10
if (myArray == NULL) exit(-1); // whoops, malloc failed, handle this error how you want
while(1)
{
int curEntry;
printf("enter a number, or 'q' to quit: ");
if (scanf("%d", &curEntry) == 1)
{
// store in the array, increment number of entries
myArray[numsEnteredSoFar++] = curEntry;
// here you can check for positives and negatives, or
// wait to do that at the end. The point of this example
// is to show how to dynamically increase memory allocation
// during runtime.
if (numsEnteredSoFar == arraySize)
{
puts("Array limit reached, reallocing");
// we've reached our limit, need to allocate more memory to continue.
// The expansion strategy is up to you, I'll just continue to add
// INITIAL_SIZE
arraySize += INITIAL_SIZE;
int* temp = realloc(myArray, arraySize * sizeof(*myArray));
if (temp == NULL)
{
// uh oh, out of memory, handle this error as you want. I'll just
// print an error and bomb out
fprintf(stderr, "out of memory\n");
exit(-1);
}
else
{
// realloc succeeded, we can now safely assign temp to our main array
myArray = temp;
}
}
}
else
{
// the user entered 'q' (or anything else that didn't match an int), we're done
break;
}
}
// print the array just to show it worked. Instead, here you can
// loop through and do your comparisons for positive and negative,
// or you can continue to track that after each entry as you've
// shown in your code
printArray(myArray, numsEnteredSoFar);
free(myArray);
return 0;
}
Demo
Several parts to the answer.
Either declare a nice, big array, bigger than you'll ever need, or, prompt the user for the size, and then use that user-entered size to declare or allocate the array. (This is a popular strategy, but it's a lousy user experience, since the user shouldn't need to know or say how many numbers they're going to enter.)
Check the return value of scanf. If the return value isn't 1, this means that scanf failed, and didn't input a number. You can take this as an indication that the user stopped entering numbers.
Have two variables: the size of the array, and the number of numbers actually entered. You set the number of numbers actually entered by noticing when scanf failed. Then, later, when you work with the date in the array, you don't do for(i = 0; i < N; i++), you do for(i = 0; i < number_of_numbers; i++).
If you don't want to ask the user to explicitly enter the number of numbers, and you don't want to pick a "big enough" size in advance (either because you don't want to waste memory, or because you want to make sure the user can enter a lot of inout, potentially more than any number you picked), it's possible to dynamically reallocate an array bigger and bigger as the user enters more and more data, but that's an advanced topic.

Why does my C compiler skip the second scanf?

My Programm looks like this.
int main(){
int maxnote = 0;
int eingabewert;
int n = 0;
int userMarks[200];
ind promark;
printf("Welcome, plese enter your points, -1 to finish.\n");
while (eingabewert != -1){
scanf("%d", &eingabewert);
if(eingabewert < -1){
printf("A student can't have 0 > points.\n");
exit(0);
}
userMarks[counter] = eingabewert;
counter += 1;
}
printf("Please insert, the least pints needed for 6:");
//Second Scanf doesn't work, it stays in a Loop or something like that
scanf(" %d", &maxnote);
for(int i = 0; userMarks[i] != -1; i++){
userMarks[i] = berechneNote(userMarks[i], maxnote);
}
countMarks(userMarks);
notenstats(userMarks);
promark = ((suffmark/counter) * 100);
printStatistic(maxnote, promark);
}
The first Scanf() does it job perfectly and takes the given numbers.
However the second one isn't doing that.
It stays in a Loop and I can't go forward with my code.
What should I do to fix this?
Because you are using eingabewert uninitialized in
while (eingabewert != -1){
Initialize it with
int eingabewert = 0;
And always check the result of scanf
while ((eingabewert != -1) && (scanf("%d", &eingabewert) == 1))
You are also using userMarks uninitialized in
for(int i = 0; userMarks[i] != -1; i++){
In this case (an array) initialize it using
int userMarks[200] = {0};
There is a space in the format specifier in the second scanf. And you should learn how to use a debugger, like gdb. It's a lot faster than posting such a long question on SO.

Weird Array Outputs

Okay what am I doing wrong here?
This program is supposed to read 20 integers and then output an array of the integers that are not duplicates (Output each integer only once).
//Program to read 20 integers and return each integer only once (no duplicates).
#include <stdio.h>
int main()
{
int a, b, count=0, temp, array1[20];
printf("Enter 20 array elements between 1 and 10 inclusive\n");
for (a=0; a<20; a++) //Loop to enter 20 elements
{
scanf("%d", &temp);
for (b=0; b<=20; b++) //Loop to test each new element against all previous entered elements
{
if (array1[b] == temp) //If duplicate increment count
{
count++;
}
else if (count == 0 && b == 20) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
{
array1[a] = temp;
}
}
}
for (a=0; a<20; a++)
{
printf("%d\t", array1[a]);
}
return 0;
}
There are the following things wrong here.
In the inner loop, during the first check, you are comparing against 20 elements. On receiving the first element you do not have any elements to compare against. I have added a variable size to indicate the size of the array. size is initialized to 0.
The if (count == 0 && b == 20) should be moved outside the for loop and can be simplified to if (count == 0)
When an element is added to the array it is added at array1[size] and size is incremented.
You need to reinitialize count at every outer for loop as shown below.
The printing will print size elements that are non duplicate.
Code is below.
//Program to read 20 integers and return each integer only once (no duplicates).
#include <stdio.h>
int main()
{
int a, b, count=0, temp, array1[20];
int size = 0;
printf("Enter 20 array elements between 1 and 10 inclusive\n");
for (a=0; a<20; a++) //Loop to enter 20 elements
{
scanf("%d", &temp);
count = 0;
for (b=0; b<size; b++) //Loop to test each new element against all previous entered elements
{
if (array1[b] == temp) //If duplicate increment count
{
count++;
}
}
if (count == 0) //If there have been no duplicates and 20 numbers have been tested... add entered number to the array
{
array1[size] = temp;
size++;
}
}
for (a=0; a<size; a++)
{
printf("%d ", array1[a]);
}
return 0;
}
This code will accept 20 elements and store and display as many as were non duplicate (which can be 1-20). If you want to store 20 non duplicate elements (entering possibly many more than 20) it can be easily modified.
You have multiple reads of uninitialized variables which is undefined behavior. You also access the array out of range.
for (b=0; b<=20; b++)
^^
This will result in b in the range [0..20]
{
if (array1[b] == temp) //If duplicate increment count
^^^^^^^^^
array1[b] is uninitialized
and when b is 20 you access out of range
Further you only write to the array when count is 0 and b is 20
else if (count == 0 && b == 20)
{
array1[a] = temp;
}
Notice that you never reset count so after the first match you'll never write the array again
BTW - you print:
Enter 20 array elements between 1 and 10 inclusive
but you never perform any check of the input value to be in that range.

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