I am tring to pass an array to a function but when i compile it says array undeclared first use in function.i thought once i pass it as parameter it becomes known by the function.here is my code:
/*
Write a program that reads quizzes from a file 'q.txt', randomly selects a number of questions,and presents them to the user one-by-one, collecting user answers. After user has answered all questions,program gives user the score.Use functions to structure your program. Extra credit is given if you use dynamic data structure(s).*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 50
#define MAX_LEN 300
void generatequiz(void)
{
static int seen[MAX];
static int array[10];
int i;
srand(time(NULL)); /* Seed the random number generator. */
for (i=0; i<10; i++)
{
int r;
do
{
r = rand() / (RAND_MAX / MAX + 1);
}
while (seen[r]);
seen[r] = 1;
array[i] = r + 1;
}
int sort(const void *x, const void *y)
{
return (*(int*)x - *(int*)y);
}
qsort(array, 10, sizeof(int), sort);
return 0;
}
void readquestions ( struct questions structered_array[]) /*reading questions to user give a score*/
{
int j,score_count=0;
char user_answer;
for (j=0;j<10;j++)
{
printf( "Press keys a or b or c or d to give your answer \n");
printf("%s\n %s\n %s\n s%\n",structured_array[j].question,
structured_array[j].choice_1,
structured_array[j].choice_2,
structured_array[j].choice_3);
user_answer=getchar();
if (user_answer= structured_array[j].answer)
score_count++;
}
printf( "Your score is %d out of 10 \n");
return 0;
}
int main()
{
generatequiz();
int array1[10];
int count = 1;
char unstructered_array[10];
char random_array1[10];
int i=0;
FILE *fp;
fp = fopen("q.txt", "r");
if (fp == NULL)
{
puts("Cannot open file for reading");
exit(EXIT_FAILURE);
}
char my_string[MAX_LEN + 1];
while (fgets(my_string, MAX_LEN + 1, stdin) != NULL)
if (count==random_array1[i])
{
struct questions
{
char question [100];
char choice_1 [50];
char choice_2 [50];
char choice_3 [50];
char answer ;
};
struct questions structered_array[10];
structered_array[i].question[100] = strtok(my_string, ":");
structered_array[i].choice_1[50] = strtok(NULL, ":");
structered_array[i].choice_2[50] = strtok(NULL, ":");
structered_array[i].choice_3[50] = strtok(NULL, ":");
structered_array[i]. answer = strtok(NULL, "\n");
count++;
i++;
}
fclose(fp);
return structered_array;
readquestions(structured_array);
return 0;
}