Reputation: 43
I'm in the process of writing a C program that, at one point, asks the user to enter 10 positive whole numbers (integers) and prints the smallest number among those 10. This section of the program cannot include the use of arrays and must include the use of a for-loop. I'm stuck because, as far as I know, it's impossible to change variable names dynamically in C. Here is what I have thus far:
int main()
{
int value1, value2, value3, value4, value5, value6, value7, value8, value9, value10;
printf("Enter 10 positive integers.\n");
scanf("%i %i %i %i %i %i %i %i %i %i", &value1, &value2, &value3, &value4, &value5, &value6, &value7, &value8, &value9, &value10);
return 0;
}
I'm new to C so any help with the rest of the code would be greatly appreciated.
Upvotes: 0
Views: 83
Reputation: 298
Yes it is possible, making use of the fact that there is little-to-no difference between arrays and pointers to arrays in C. Basically you get a pointer to the first variable and then iterate through them, just like as if it was an array. The only thing to watch out for is to guarantee a known alignment of your variables. To achieve that, you have to put them in a struct.
I wasn't sure if your requirement was to only avoid the declaration of an array or completely avoid array syntax, so I avoided array syntax in the example below:
#include <stdio.h>
int main()
{
struct {
int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
} inputs;
printf("Enter 10 positive integers.\n");
scanf("%i %i %i %i %i %i %i %i %i %i", &inputs.v1, &inputs.v2, &inputs.v3, &inputs.v4, &inputs.v5, &inputs.v6, &inputs.v7, &inputs.v8, &inputs.v9, &inputs.v10);
const int numItems = 10; // how many inputs do we have
int *ptr = &inputs.v1; // get pointer to first input
int smallest = *ptr; // initialize our result before calculation
++ptr; // increment pointer to second element
for (int i = 1; i < numItems; ++i) // we start from the second element so we start start counting from 1
{
// Smallest element calculation
if (*ptr < smallest)
smallest = *ptr;
++ptr;
}
printf("Smallest number: %i.\n", smallest);
return 0;
}
Here is a nicer looking version with array syntax (but still not using arrays):
#include <stdio.h>
int main()
{
struct {
int v1, v2, v3, v4, v5, v6, v7, v8, v9, v10;
} inputs;
printf("Enter 10 positive integers.\n");
scanf("%i %i %i %i %i %i %i %i %i %i", &inputs.v1, &inputs.v2, &inputs.v3, &inputs.v4, &inputs.v5, &inputs.v6, &inputs.v7, &inputs.v8, &inputs.v9, &inputs.v10);
const int numItems = 10; // how many inputs do we have
int* ptr = &inputs.v1; // get pointer to first input
int smallest = ptr[0]; // initialize our result before calculation
for (int i = 1; i < numItems; ++i) // we start from the second element so we start start counting from 1
{
// Smallest element calculation
if (ptr[i] < smallest)
smallest = ptr[i];
}
printf("Smallest number: %i.\n", smallest);
return 0;
}
Upvotes: 1
Reputation: 108988
Simple example
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int minimum;
printf("Input 10 space separated integers\n");
if (scanf("%d", &minimum) != 1) exit(EXIT_FAILURE);
for (int j = 0; j < 9; j++) {
int value;
if (scanf("%d", &value) != 1) exit(EXIT_FAILURE);
if (value < minimum) minimum = value;
}
printf("minimum value entered was %d.\n", minimum);
}
Upvotes: 3
Reputation: 5523
Not in the manner you have written it.
If you were getting input one by one then you could keep a running track of what's the min value you have seen so far.
Upvotes: 2