Files and generating numbers ,c programming -
task next: after generate 20 random numbers , read informations file tombula.txt contains names of users , 9 numbers,print first user (his/her name) has 3 numbers own array equal generated numbers.
this part of code generate numbers , read file don't know how find first person has 3 numbers array equal generated numbers,if know how write please help:
#define _crt_secure_no_warnings #include<stdio.h> #include<stdlib.h> #include <time.h> typedef struct { char name[50]; int num[8]; }someone; void generating(int[]); int check(int[], int); int main(void) { *pk = null; int i, j, count = 0, tip; file *f; int generated[30]; int brojac[5]; srand(time(null)); generating(generated); (int j = 0; j < 20; j++) printf("%d ", generated[j]); printf("\n\n\n"); f = fopen("tombula.txt", "r"); if (f == null) { printf("error!"); exit(1); } = 0; while (feof(f) == 0) { pk = ((someone *)realloc(pk, (i + 1)*sizeof(someone))); fscanf(f, "%s", pk[i].name); (j = 0; j < 8; j++) fscanf(f, "%d", &pk[i].num[j]); i++; count++; } i--; count--; (int z = 0; z < count; z++){ printf("%s ", pk[z].name); (int k = 0; k < 8; k++){ printf("%d ", pk[z].num[k]); } printf("\n"); } return 0; } void generating(int numbers[]) { int i, tmp; (i = 0; < 20; ++i) { tmp = rand() % 50 + 1; if (check(numbers, tmp)) { --i; continue; } numbers[i] = tmp; } } int check(int b[], int a){ int i; int n = 8; (i = 0; < 20; ++i) { if (b[i] == a) return 1; } return 0; }
1.you opened file in r
mode.
2.in loop (loop must work until eof) read data stored in file using fscanf()
.store name
char array
, number
integer array
(as defined in struct).
3.inside loop check if 3 numbers
matches numbers generated
, if print
persons name
, use break
come out of loop.
4.after close
file.
Comments
Post a Comment