I am making a C program which aims to input numbers and store them in an array, but I have as a restriction that the same number cannot be entered twice. Any idea of how to implement it?
Here is my code:
#include<stdlib.h>
#include <stdio.h>
int main(){
int n=0, num=0, i, j;
int N[n];
printf("Write the amount of values that you will introduce\n");
do {
scanf("%d",&n);
if(n<0)
printf("Invalid number, try again\n");
} while (n<0);
printf("Remember to use numbers between 0 and 50\n");
for(i=0; i<n; i ) {
do {
scanf("%d",&num); //If a repeated value is entered, show a message and request the number again
if(num<0 || num>50){
printf("Invalid number, try again\n");
}else{
N[i]=num;
printf("- - - - -\n");
}
} while (num<0 || num>50);
getchar();
getchar();
return 0;
}
}
CodePudding user response:
Because your value range is very small, it's very easy to record the fact that a number is in your list by setting a single bit.
Indeed, you have 51 possible values in the range [0, 50] so you can represent these with a 64-bit integer. Standard integer types are in <stdint.h>
, where you'll find uint64_t
.
uint64_t in_list = 0;
int numbers[51];
int count = 0;
for(int num; count <= 50 && 1 == scanf("%d", &num); )
{
if (num >= 0 && num <= 50) {
if (!(in_list & (1ULL << num))) {
numbers[count ] = num;
in_list |= (1ULL << num);
} else {
printf("Value %d is already added.\n", num);
}
}
}
There is still an unpleasant amount of hard-coded values here, but that can be improved, and even extended for higher ranges. For simplicity, let's assume zero is always the minimum. And since we'll need an array for the bits, we don't even have to use 64-bit values. Simply characters are fine.
#include <stdint.h>
#include <stdio.h>
#define MAX_NUM 250
int main()
{
uint8_t in_list[(MAX_NUM / 8) 1] = { };
int numbers[MAX_NUM 1];
int count = 0;
for(int num; count <= MAX_NUM && 1 == scanf("%d", &num); )
{
if (num >= 0 && num <= MAX_NUM) {
int char_select = num / 8;
int bit_mask = 1 << (num % 8);
if (!(in_list[char_select] & bit_mask)) {
numbers[count ] = num;
in_list[char_select] |= bit_mask;
} else {
printf("Value %d is already added.\n", num);
}
} else {
printf("Enter a value between 0 and %d.\n", MAX_NUM);
}
}
}
CodePudding user response:
When the user enters a new number, use a for
loop to look at all numbers already in the array. If you find that the number is already in the array, then make them enter another number. So your inner loop would look something like this:
while (1) {
scanf("%d",&num);
// Validation #1: value
if (num < 0 || num > 50) {
printf("Numero no valido, ingrese de nuevo el valor\n");
continue;
}
// Validation #2: uniqueness
bool duplicate = false;
for (int k = 0; k < i; k ) {
if (N[k] == num) {
printf("Number is a duplicate.\n");
duplicate = true;
break;
}
}
if (duplicate) { continue; }
N[i] = num;
break;
}
Note that we use continue
to jump back to the top of the current loop, and break
to jump out of it. There is no easy way to break
or continue
the loop one level above the current one (unless you allow the use of goto
), so that is why I have to use a boolean flag named duplicate
.
By the way, you might need to include stdbool.h
to use the bool
type.
CodePudding user response:
Once the user adds a value, push it to an array. Then the user inputs a value again, go through the array to check if they value already exists. If it exists, then the user has already input the value before. In that case don't let the user input the same value. If not, let them. SIMPLE :)