in my program the user enters an array and the pc generates a random array (with the max number picked from user) and then the program creates a third array that shows the number from the first array that are not in the second . however i only want to type the numbers once each but the program types them again and again until array is full .
for example if i entered 0 3 1 and generated number was 1 5 9 it would print 0 3 3
i want to print 0 3 only .
full code :
#include <stdio.h>
#define N 7
void input(int ar1[N]);
void max(int* mx);
int input2(int ar2[N], int mx);
int input3(int ar3[N], int ar1[N], int ar2[N]);
int main()
{
int ar1[N], mx, ar2[N], ar3[N], i;
input(ar1);
max(&mx);
input2(ar2, mx);
input3(ar3, ar1, ar2);
printf("array1 = %d %d %d %d %d %d %d\n", ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6]);
printf("array2 = %d %d %d %d %d %d %d\n", ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6]);
printf("array3 = %d %d %d %d %d %d %d\n", ar3[0], ar3[1], ar3[2], ar3[3], ar3[4], ar3[5], ar3[6]);
}
void input(int ar1[N])
{
int i;
printf("Enter 7 numbers: ");
for (i = 0; i < N; i++)
scanf("%d", &ar1[i]);
}
void max(int* mx)
{
printf("Enter random number bigger than 0 :- ");
scanf("%d", mx);
if (mx < 0)
{
printf("you have entered a number smaller than 0 please enter a number bigger than 0 :-");
scanf("%d", mx);
}
return mx;
}
int input2(int ar2[N], int mx)
{
int i;
srand(time(0));
for (i = 0; i < N; i++)
{
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
return ar2;
}
int input3(int ar3[N], int ar1[N], int ar2[N])
{
int i, j, a = 0;
for (i = 0; i < N; i++)
{
if (ar1[i] != ar2[0] && ar1[i] != ar2[1] && ar1[i] != ar2[2] && ar1[i] != ar2[3] && ar1[i] != ar2[4] && ar1[i] != ar2[5] && ar1[i] != ar2[6])
a = ar1[i];
ar3[i] = a;
}
return ar3;
}
Thanks for your help.
the following proposed code:
handles the problems exposed in the comments to the question
cleanly compiles
has some 'appropriate' horizontal spacing added for readability
caveat: I have not verified that the OPs posted logic is correct.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 7
// prototypes
void input ( int ar1[N] );
void max ( unsigned* mx );
void input2( int ar2[N], int mx );
void input3( int ar3[N], int ar1[N], int ar2[N] );
int main( void )
{
int ar1[N];
int mx;
int ar2[N];
int ar3[N];
srand( (unsigned)time(0) );
input( ar1 );
max( (unsigned*)&mx );
input2( ar2, mx );
input3( ar3, ar1, ar2 );
printf( "array1 = %d %d %d %d %d %d %d\n",
ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6] );
printf( "array2 = %d %d %d %d %d %d %d\n",
ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6] );
printf( "array3 = %d %d %d %d %d %d %d\n",
ar3[0], ar3[1], ar3[2], ar3[3], ar3[4], ar3[5], ar3[6] );
} // end function: main
void input( int ar1[N] )
{
printf( "Enter 7 numbers: " );
for ( size_t i = 0; i < N; i++ )
{
if( 1 != scanf("%d", &ar1[i]) )
{
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
}
} // end function: input
void max( unsigned* mx )
{
printf("Enter random number bigger than 0 :- ");
if( 1 != scanf("%u", mx) )
{
perror( "scanf failed" );
exit( EXIT_FAILURE );
}
} // end function: max
void input2( int ar2[N], int mx )
{
for ( size_t i = 0; i < N; i++ )
{
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
} // end function: input2
void input3( int ar3[N], int ar1[N], int ar2[N] )
{
int a = 0;
for (size_t i = 0; i < N; i++)
{
if( ar1[i] != ar2[0]
&& ar1[i] != ar2[1]
&& ar1[i] != ar2[2]
&& ar1[i] != ar2[3]
&& ar1[i] != ar2[4]
&& ar1[i] != ar2[5]
&& ar1[i] != ar2[6] )
a = ar1[i];
ar3[i] = a;
}
} // end function: input3
IIUC your problem can be reduced to removing duplicates from the array. I tried to fix it and hope that it works for you:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#define N 7
void input(int ar1[N]);
int max(int * mx);
int input2(int ar2[N], int mx);
static int compact(int *array, int size)
{
int i;
int last = 0;
assert(size >= 0);
if (size <= 0)
return size;
for (i = 1; i < size; i++)
{
if (array[i] != array[last])
array[++last] = array[i];
}
return(last + 1);
}
int i, a = 0;
int j = 0;
static void print(int *array, int size, const char *tag, const char *name)
{
int k;
printf("%s\n", tag);
for (k = 0; k < i-j; k++)
printf("%s[%d] = %d\n", name, k, array[k]);
}
int * input3(int ar3[N], int ar1[N], int ar2[N]);
int main() {
int ar1[N], mx, ar2[N], ar3[N], i;
input(ar1);
max(&mx);
input2(ar2, mx);
input3(ar3, ar1, ar2);
int a_size = sizeof(ar3) / sizeof(ar3[0]);
a_size = compact(ar3, a_size);
print(ar3, a_size, "\nAnswer:", "a");
printf("array1 = %d %d %d %d %d %d %d\n", ar1[0], ar1[1], ar1[2], ar1[3], ar1[4], ar1[5], ar1[6]);
printf("array2 = %d %d %d %d %d %d %d\n", ar2[0], ar2[1], ar2[2], ar2[3], ar2[4], ar2[5], ar2[6]);
}
void input(int ar1[N]) {
int i;
printf("Enter %d numbers: ", N);
for (i = 0; i < N; i++)
scanf("%d", &ar1[i]);
}
int max(int *mx) {
printf("Enter random number bigger than 0 :- ");
scanf("%d", mx);
if (mx < 0) {
printf("you have entered a number smaller than 0 please enter a number bigger than 0 :-");
scanf("%d", mx);
}
return * mx;
}
int input2(int ar2[N], int mx) {
int i;
srand(time(0));
for (i = 0; i < N; i++) {
ar2[i] = 0 + rand() % (mx - 0 + 1);
printf("%d ", ar2[i]);
}
return * ar2;
}
int * input3(int ar3[N], int ar1[N], int ar2[N]) {
for (i = 0; i < N; i++) {
if (ar1[i] != ar2[0] && ar1[i] != ar2[1] && ar1[i] != ar2[2] && ar1[i] != ar2[3] && ar1[i] != ar2[4] &&
ar1[i] != ar2[5] && ar1[i] != ar2[6]) {
//a = ar1[i];
ar3[i-j] = ar1[i];
}
else {
++j;
continue;
}
}
return ar3;
}
Test
Enter 7 numbers: 2 4 6 8 9 11 12
Enter random number bigger than 0 :- 12
3 12 1 6 2 8 9
Answer:
a[0] = 4
a[1] = 11
array1 = 2 4 6 8 9 11 12
array2 = 3 12 1 6 2 8 9
Related
This is my code, it works but i feel i could do something different
#include <stdio.h>
int main()
{
int n;
printf("Input a number [0,9]: ");
scanf("%d", &n);
printf("*****\n");
printf("*****\n");
printf("**%d", n);
printf("**");
printf("\n*****\n");
printf("*****\n");
return 0;
}
Is this the best solution or is there something easier?
I tend to do 1 printf() per output line
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
printf("Input a number [0,9]: ");
if (scanf("%d", &n) != 1) exit(EXIT_FAILURE);
if ((n < 0) || (n > 9)) printf("nope\n");
else {
printf("*****\n");
printf("*****\n");
printf("**%d**\n", n);
printf("*****\n");
printf("*****\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n;
printf("Input a number [0,9]: ");
if (scanf("%d", &n) != 1) exit(EXIT_FAILURE);
if ((n < 0) || (n > 9)) printf("nope\n");
else {
for (int i = 0; i < 5; i++) {
if (i == 2) {
printf("**%d**\n", n);
} else {
printf("*****\n");
}
}
}
return 0;
}
you can do it a bit more universal way. It will print NCHARS around the number and NLINES lines of FILLER around the number. Any number is covered (including negave ones)
#include <stdio.h>
#define NCHARS 3
#define NLINES 3
#define FILLER '*'
size_t countdigits(int n)
{
size_t ndigits = n <= 0 ? 1 : 0;
while(n)
{
ndigits++;
n /= 10;
}
return ndigits;
}
void printlines(size_t nchars, char ch)
{
for(size_t before = 0; before < NLINES; before++)
{
for(size_t pos = 0; pos < nchars; pos++ )
printf("%c", ch);
printf("\n");
}
}
void printfsurrounded(int n)
{
size_t nchars = countdigits(n);
printlines(nchars + NCHARS * 2, FILLER);
for(int pos = 0; pos < NCHARS; pos++) printf("%c", FILLER);
printf("%d", n);
for(int pos = 0; pos < NCHARS; pos++) printf("%c", FILLER);
printf("\n");
printlines(nchars + NCHARS * 2, FILLER);
}
int main(void)
{
printfsurrounded(4);
printf("\n");
printfsurrounded(1024);
printf("\n");
printfsurrounded(-233445);
}
https://godbolt.org/z/hz1eq9
I have an university project in C programming. I ran into a problem with the following task. My program should order numbers in two arrays. In the first array i must save (the biggest of every fifth element) and that is my problem. I am not sure how to make the loop which reads five elements compare them, take the biggest one, and then continue doing this with the other elements. I am hoping someone to help because I blocked.
#define A 100
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
void show(int x[], int nx); //функция за показване на масивите
float vavedi(int x[], int nx); //функция, чрез която ръчно въвеждаме числата и ги обработваме
FILE* readFile(char* fname); //функция която чете файл и представя съдържанието му като масиви
int main()
{
int call, a = 0, b = 0, mode = 0, i = 0;
int check = 0;
char fail[A];
char* menu[] = {
"PROGRAM STARTED!",
"Enter an option:",
"1 : Write the numbers.",
"2 : Choose from a file.",
"0 : Exit."
};
do {
for (i = 0; i < 5; i++)
printf("%s\n", menu[i]);
check=scanf("%d", &mode);
if (check != 1)
printf("ERROR! Try again!");
switch (mode)
{
case 1: {
//в случай 1 числата се въведждат от потребителя
call = vavedi(a, b);
break;
}
case 2: {
//в случай 2 потребителя използва съществуващ файл
printf("Enter the path of the file you want to open:\n");
scanf("%s", fail);
call = readFile(fail);
if (call == NULL) {
printf("The file doesn't exist! Try again!\n");
}
break;
}
case 0:
break;
default:
{
printf("ERROR! Try again!\n");
}
}
} while (mode != 0);
printf("\nThe program ended!\n");
return 0;
}
void show(int x[], int nx)
{
int k;
for (k = 0; k < nx; k++)
{
printf("\n Element[%d]= %d", k, x[k]);
}
}
float vavedi(int x[], int nx)
{
int call=0;
int enter=0;
int imin, max;
int b[A], c[A];
int i, j, j1, count;
j = 0;
do {
printf("\nCount of the elements:");
scanf("%d", &count);
if (count <= 0 || count > 100)
printf("Invalid input! Try again!\n");
} while (count <= 0 || count > 100);
for (i = 0; i < count; i++)
{
printf("\nEnter an element:");
scanf("%d", &c[i]);
}
printf("\n");
return enter;
}
Update : Including header file mentioned by #pmg.
If i understand correctly, your problem is to find largest element for every five elements:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <limits.h> // Put this after your header files
..
.. //Rest of the code.
..
for (i = 0; i < count; i++)
{
printf("\nEnter an element:");
scanf("%d", &c[i]);
}
size_t size = ceil(count/5.0);
memset(b , INT_MIN , size);
for(int i = 0 ; i < count ; i++)
{
b[i/5] = b[i/5] > c[i] ? b[i/5] : c[i];
}
// Print b like this:
for(i = 0 ; i < size; i++)
printf("%d" , b[i] );
Let's see how this works for array of size 9. array's indices will go from 0 to 8 . If I divide each of them by 5, I get 0 for i = 0 to 4 and 1 for i = 5 to 8. Now we can take max over elements in buckets of 5. You seem to be a beginner, hope this helps you in creating better understanding.
The other problem is the following:
{
int c[A], b[A];
int i = 0, j = 0, k = 0, num;
FILE* fp= NULL; //указател за файл
fp = fopen(fname, "r");
if (fp)
{
while (fscanf(fp, "%d", &c[i]) != EOF)
i++;
num=i;
size_t size = ceil(num/5.0);
memset(b , INT_MIN , size);
for(i = 0 ; i < num ; i++)
{
b[i/5] = b[i/5] > c[i] ? b[i/5] : c[i];
}
printf("\nARRAY1:\n");
for(i = 0 ; i < size; i++)
{
buble(b, i);
printf(" Element[%d]=%1d\n", i, b[i]);
}
printf("\nARRAY2:");
buble(c, num);
show(c, num);
printf("\n");
}
fclose(fp);
return fp;
}
It doesn't calculate the rigth way.
So what my codes does at the moment is quite simple. It receives an input like this:
5 4
1 2
2 3
3 4
4 5
The first line of the input represents the number of Ids and the number of relations. The following line are those relations. The program sorts these pairs, for example: 1 comes before 2, 2 comes before 3 and so on. In this case the outup will be 1 2 3 4 5.
In case of contradictive input, such as:
4 4
1 2
2 3
3 2
3 4
It returns "Incoerente".
My current problem is my input is "non conclusive" such as:
4 4
1 2
3 1
3 4
4 2
In this case the program doesnt really know where to place the 4 (after or before the 1?). My program right now (with the input above) will print "3 1 2", and ignores the 4 for some reason. When this situation apears I want to print "OTHERERROR".
Here is my code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void create_graph();
void add(int vertex);
int del();
int isEmpty();
int find_indegree_of_vertex(int vertex);
int total_vertices;
int adjacent_matrix[MAX][MAX];
int queue[MAX];
int front = -1;
int rear = -1;
FILE *myFile;
int main(){
myFile = fopen("input.txt", "r");
int i;
int numberArray[2];
for(i=0;i<2;i++)
fscanf(myFile, "%1d", &numberArray[i]);
int N = numberArray[0];
int L = numberArray[1];
int vertex, count, topological_sort[MAX], indegree[MAX];
create_graph(N, L);
for(i = 0; i < total_vertices; i++){
indegree[i] = find_indegree_of_vertex(i);
if(indegree[i] == 0){
add(i);
}
}
count = 0;
while(!isEmpty() && count < total_vertices){
vertex = del();
topological_sort[++count] = vertex;
for(i = 0; i < total_vertices; i++){
if(adjacent_matrix[vertex][i] == 1){
if(adjacent_matrix[i][vertex] == 1){
printf("INCOERENTE !!!!!!!!!!!!!!!\n");
}
adjacent_matrix[vertex][i] = 0;
indegree[i] = indegree[i] - 1;
if(indegree[i] == 0){
add(i);
}
}
}
}
if(count < total_vertices){
printf("Incoerente\n");
exit(1);
}
printf("Topological Order of Vertices\n");
for(i = 1; i <= count; i++){
printf("%3d", topological_sort[i]+1);
}
printf("\n");
return 0;
}
void add(int vertex){
if(rear == MAX - 1){
printf("Queue Overflow\n");
}
else{
if (front == -1) {
front = 0;
}
rear = rear + 1;
queue[rear] = vertex ;
}
}
int isEmpty(){
if(front == -1 || front > rear ){
return 1;
}
else{
return 0;
}
}
int del(){
int element;
if (front == -1 || front > rear){
printf("Queue Underflow\n");
exit(1);
}
else{
element = queue[front];
front = front+1;
return element;
}
}
int find_indegree_of_vertex(int vertex){
int count, total_indegree = 0;
for(count = 0; count < total_vertices; count++){
if(adjacent_matrix[count][vertex] == 1){
total_indegree++;
}
}
return total_indegree;
}
void create_graph(int N, int L){
int count, maximum_edges, origin_vertex, destination_vertex;
total_vertices = N;
maximum_edges = L;
int integers[MAX];
int i =0;
int num;
while(fscanf(myFile, "%d", &num) > 0) {
integers[i] = num;
i++;
}
i=0;
printf("1a linha :%d %d\n", N, L);
for(count = 1; count <= maximum_edges; count++){
origin_vertex = integers[i]-1;
destination_vertex = integers[i+1]-1;
printf("pares a ser avaliados :%d %d\n", origin_vertex+1, destination_vertex+1);
if(origin_vertex > total_vertices || destination_vertex > total_vertices || origin_vertex < 0 || destination_vertex < 0){
printf("Edge Co-ordinates are Invalid\n");
count--;
}
else
adjacent_matrix[origin_vertex][destination_vertex] = 1;
if(count == maximum_edges){
break;
}
i+=2;
}
}
I have this function to find the max and min value of numbers in file with uknown text("ADS 50 d 15"). It works fine with only digits in the file, but when there is a characters it just stops.
{
int n;
int min = INT_MAX, max = INT_MIN;
int flag = 0;
rewind(f);
while (fscanf(f, "%d", &n) != EOF)
{
if (ferror(f))
{
perror("Error:");
}
if (flag == 0)
{
min = n;
max = n;
flag = 1;
}
if (min>n)
min = n;
if (max<n)
max = n;
}
printf("\nMax value: %d\nMin value: %d\n", max, min);
}
fscanf will return EOF after reaching the end of the file. It will return 1 on successful scanning an integer. If the input is not an integer, it will return 0 and the problem input has to be removed.
{
int n;
int min = INT_MAX, max = INT_MIN;
int result = 0;
char skip = 0;
rewind ( f);
while ( ( result = fscanf ( f, "%d", &n)) != EOF)
{
if (result == 0)
{
fscanf ( f, "%c", &skip);//remove a character and try again
}
else
{
if (min>n)
min = n;
if (max<n)
max = n;
}
}
printf("\nMax value: %d\nMin value: %d\n", max, min);
Try the following approach as it is shown in this demonstrative program. You have to use the fscanf instead of scanf used in this program.
#include <stdio.h>
#include <ctype.h>
int main( void )
{
int min, max;
size_t n = 0;
while ( 1 )
{
char c;
int x = 0;
int success = scanf( "%d%c", &x, &c );
if ( success == EOF ) break;
if (success != 2 || !isspace( ( unsigned char )c ) )
{
scanf("%*[^ \t\n]");
clearerr(stdin);
}
else if ( n++ == 0 )
{
min = max = x;
}
else if ( max < x )
{
max = x;
}
else if ( x < min )
{
min = x;
}
}
if ( n )
{
printf( "\nThere were enetered %zu values\nmax value: %d\nMin value: %d\n",
n, max, min );
}
return 0;
}
If the input looks like
1 2 3 4 5a a6 7 b 8
then the output will be
There were enetered 6 values
max value: 8
Min value: 1
I have a problem thats giving me a huge ache.
This piece of code purpose is to fill up an array with integer values and at the same time defend against strings and etc....but it doesn't defend against duplicates, but tried I got to far as replacing the number with a new number for example
Enter 6 integers
1, 2, 2, 3, 4, 5
my code will let me replace that 2 at position 1 with another number. What I want it to do is not to repeat the same number again, for example please replace 2 at position 1. I dont want the user to enter 2 again... and I want to make it to double check the work the array if any repeating numbers exists thank you.
system("clear");
printf("\nEntering Winning Tickets....\n");
nanosleep((struct timespec[]){{1, 0}}, NULL);
system("clear");
char userInput[256];
char c;
int duplicationArray[6] = {-1, -1, -1, -1, -1, -1};
for (i = 0; i < 6; i++)
{
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, 256, stdin);
if ((sscanf(userInput, "%d %c", &winningNumbers[i], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}
for (i = 0; i < 6 - 1; ++i)
{
min = i;
for (j = i+1; j < 6; ++j)
{
if (winningNumbers[j] < winningNumbers[min])
min = j;
}
temp = winningNumbers[i];
winningNumbers[i] = winningNumbers[min];
winningNumbers[min] = temp;
}
for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;
}
else
{
duplicationCounter--;
}
}
if (duplicationCounter > -6)
{
for (i = 0; i < 6; i++)
{
int j, min, temp;
min = i;
for (j = i+1; j < 6; ++j)
{
if (duplicationArray[j] > duplicationArray[min])
min = j;
}
temp = duplicationArray[i];
duplicationArray[i] = duplicationArray[min];
duplicationArray[min] = temp;
}
for (i = 0; i < 6; i++)
{
if (duplicationArray[i] == -1)
{
zeroCounter++;
}
}
int resize = (6 - zeroCounter)+1;
for (i = 0; i <= resize; i++)
{
if (duplicationArray[i] == -1)
{
i++;
}
else if (duplicationArray[i] != -1)
{
system("clear");
printf("\nDuplicated numbers has been dected in your array. ");
printf("\nPlease replace the number %d at postion %d with another number: ", winningNumbers[duplicationArray[i]], duplicationArray[i]);
fgets(userInput, 256, stdin);
if ((sscanf(userInput, "%d %c", &winningNumbers[duplicationArray[i]], &c) != 1 || (winningNumbers[i] <= 0) || winningNumbers[i] >= 50))
{
printf("\nInvalid Input.\n") ;
nanosleep((struct timespec[]){{0, 350000000}}, NULL);
system("clear");
i = i - 1;
}
}
}
duplicationCounter = 0;
for (i = 0; i < 6; i++)
{
if (winningNumbers[i] == winningNumbers[i+1])
{
duplicationArray[i] = i;
duplicationCounter++;
}
else
{
duplicationCounter--;
}
}
printf("%d, ", duplicationCounter);
}
#include <stdio.h>
#include <stdint.h>
#define DATA_SIZE 6
int main(void){
char userInput[256];
int inputNum, winningNumbers[DATA_SIZE];
uint64_t table = 0;
int i=0;
while(i<DATA_SIZE){
printf("\nPlease enter the %d winning ticket number!(#'s must be between 1-49): ", i+1);
fgets(userInput, sizeof(userInput), stdin);
if(sscanf(userInput, "%d", &inputNum) != 1 || inputNum <= 0 || inputNum >= 50)
continue;
uint64_t bit = 1 << inputNum;
if(table & bit)
continue;
table |= bit;
winningNumbers[i++] = inputNum;
}
for(i=0;i<DATA_SIZE;++i)
printf("%d ", winningNumbers[i]);
printf("\n");
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define DATA_SIZE 6
int inputNumberWithRangeCheck(const char *msg, const char *errMsg, int rangeStart, int rangeEnd){
char inputLine[256];
int n;
for(;;){
printf("%s", msg);
fgets(inputLine, sizeof(inputLine), stdin);
if(sscanf(inputLine, "%d", &n) != 1 || n < rangeStart || n > rangeEnd)
fprintf(stderr, "%s", errMsg);
else
return n;
}
}
int inputNumber(void){
return inputNumberWithRangeCheck(
"\nPlease enter the winning ticket number!(#'s must be between 1-49): ",
"Invalid Input.\n",
1,49);
}
int *inputArray(int *array, size_t size){
int i;
for(i=0;i<size;++i){
printf("\nInput for No.%d\n", i+1);
array[i] = inputNumber();
}
return array;
}
int **duplicateCheck(int *array, size_t size){
int **check, count;
int i, j;
check = malloc(size*sizeof(int*));
if(!check){
perror("memory allocate\n");
exit(-1);
}
//There is no need to sort the case of a small amount of data
//(Cost of this loop because about bubble sort)
for(count=i=0;i<size -1;++i){
for(j=i+1;j<size;++j){
if(array[i] == array[j]){
check[count++] = &array[i];
break;
}
}
}
check[count] = NULL;
if(count)
return check;
else {
free(check);
return NULL;
}
}
int main(void){
int winningNumbers[DATA_SIZE];
int **duplication;
int i, j;
inputArray(winningNumbers, DATA_SIZE);
while(NULL!=(duplication = duplicateCheck(winningNumbers, DATA_SIZE))){
for(i=0;i<DATA_SIZE;++i){
if(duplication[i]){
printf("\nyour input numbers : ");
for(j=0;j<DATA_SIZE;++j)
printf("%d ", winningNumbers[j]);
fprintf(stderr, "\nThere is duplicate. Please re-enter.\n");
*duplication[i] = inputNumber();
} else
break;
}
free(duplication);
}
for(i=0;i<DATA_SIZE;++i)
printf("%d ", winningNumbers[i]);
printf("\n");
return 0;
}