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 enter image description here
#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;
}
CodePudding user response:
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.
CodePudding user response:
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 thatscanf
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 dofor(i = 0; i < N; i )
, you dofor(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.
CodePudding user response:
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;
}