C program -- need an explanation

In summary: Enter a number (1, 2 or 3): "); scanf("%d",&option); switch(option) { case 1: tnq++; printf("The first question is %s.\n",q1.questionText); break; case 2: tncq++; printf("The second question is %s.\n",q2.questionText); break; case 3: printf("You failed!\n"); break; default: printf("Invalid input.\n"); } }while(option!='
  • #1
gruba
206
1

Homework Statement


I need help with the following problem in C programming language:
Define a quiz and add two questions in it with given possible answers. Display the questions one by one and allow the user to choose between 1, 2 or 3 (numbers that represent serial numbers of possible answers). If the user enters the character which is not 1, 2 or 3, then allow the user to choose an answer again. At the end of the quiz, print the final score in percentage.

Use the following definitions:
Code:
#define MAX 1000000

typedef struct
{
 char questionText[MAX];
 char firstAnswer[MAX];
 char secondAnswer[MAX];
 char thirdAnswer[MAX];
 int snca;//serial number of the correct answer (1, 2 or 3)
}QUESTION;

typedef struct
{
 int tnq;//total number of questions
 QUESTION *questions;
}QUIZ;

void addNewQuestion(QUIZ *qz,QUESTION *qn);//adds new question
void displayQuestion(QUIZ *qz,int snq);//prints question. 'snq' is the serial number of a question
int isCorrect(QUIZ *qz,int snq,int snqa);//checks if the chosen answer with the serial number 'snqa'
 //on a question with the serial number 'snq' is correct

Code:
/*Example program:

1. In what year was the C programming language created?

1. 1845
2. 1832
3. 1972

Choose an answer: 3

2. Who is the author of C programming language?

1. Bill Gates
2. Dennis Ritchie
3. Steve Jobs

Choose an answer: 10
Error. Choose between 1, 2 or 3.
Choose an answer: 2

Final score: 100%*/

2. The attempt at a solution

I don't understand a couple of things in this problem:

1. How to add a question using the prototype function given above?
The function requires to only add one question, so do we need two separate functions for adding a question?

2. In the structure quiz, variable tnq which represents total number of questions is given. Why? It is said in the problem that the total number of questions is two, so the program should end after the user gives an answer to two questions, right?

Could someone explain this?
 
Physics news on Phys.org
  • #2
void addNewQuestion(QUIZ *qz,QUESTION *qn);//adds new question

1. How to add a question using the prototype function given above?
Presumably you need to write this function. (I would very much hope so!) Then when you call this function, it adds a question. You call it every time you want to add a question. So you add any number of questions, BUT you need some questions to add. You can't add a question until you have a question to add (and until you have a quiz to add it to.) There is no indication of where these questions come from.

It is said in the problem that the total number of questions is two, so the program should end after the user gives an answer to two questions, right?
Wrong. It says,
Define a quiz and add two questions
It does not say there are two questions. There are none when you create it. Then you add two. You could add more.
2. In the structure quiz, variable tnq which represents total number of questions is given.
The name and type of the variable tnq is given, but its value is not given.
 
  • #3
Where to define the text of the questions? In the function addNewQuestion() or in main function?
 
  • #4
Yes, I wondered that.
You could store the data in a file (text) and read them in. But you may not have done file handling yet.
You could set them as literals in the program, though I don't know what good practice is here.
You could ask someone to type them in ie. have a function that asks for a question, accepts text from the keyboard, asks for the first answer,accepts text, asks for the second ... , asks which number is the correct answer, placing each answer into the QUESTION (after validation etc.) then adds the QUESTION to the QUIZ.

My preference would be the first. The third is tedious. The second may be the simplest, but it's not my style preference.
 
  • #5
EDIT:

Here is how I did it using singly linked list:
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 100000

typedef struct
{
  char text[MAX];
  char first[MAX],second[MAX],third[MAX];
  int sn;//serial number of a question
  int snca;//serial number of the correct answer
}QUESTION;

typedef struct list_node
{
  QUESTION qn;
  struct list_node *next;
}LIST_NODE;

void insertion(LIST_NODE **head,QUESTION *qn)
{
  LIST_NODE *newNode=(LIST_NODE *)malloc(sizeof(LIST_NODE));
  newNode->qn=*qn;
  if(*head == 0 || (*head)->qn.sn > qn->sn)
  {
  newNode->next=*head;
  *head=newNode;
  }
  else
  {
  LIST_NODE *p;
  for(p=*head;p->next && p->next->qn.sn < qn->sn;p=p->next);
  newNode->next=p->next;
  p->next=newNode;
  }
}

LIST_NODE* searching(LIST_NODE *head,int sn)
{
  while(head && head->qn.sn != sn)
  head=head->next;
  return head;
}

int main()
{
  LIST_NODE *head=0;
  char option;
  int sn;
  int tnq=0;//total number of questions
  int tncq=0;//total number of correct questions
  float result;
  QUESTION q1={"In what year was the C programming language created?","1852","1872","1972",1,3};
  QUESTION q2={"Who is the author of C programming language?","Bill Gates","Dennis Ritchie","Steve Jobs",2,2};
  QUESTION qn;

  printf("Option 'A': Insert the first question.\n");
  printf("Option 'B': Insert the second question.\n");
  printf("\n");

  do
  {
  printf("Choose an option: ");
  scanf("%c",&option);
  fflush(stdin);

  if(option == 'A')
  {
  LIST_NODE *t1=searching(head,q1.sn);
  if(t1){
  t1->qn=q1;
  tnq++;}
  else{
  insertion(&head,&q1);
  tnq++;}
  }

  else if(option == 'B')
  {
  LIST_NODE *t2=searching(head,q2.sn);
  if(t2){
  t2->qn=q2;
  tnq++;}
  else{
  insertion(&head,&q2);
  tnq++;}
  }

  else if(option == 'P')
  {
  int answer;
  printf("Enter serial number of a question: ");
  scanf("%d",&sn);
  LIST_NODE *p=searching(head,sn);
  if(p)
  {
  printf("%d %s\n 1. %s\n 2. %s\n 3. %s\n",
  p->qn.sn, p->qn.text, p->qn.first, p->qn.second, p->qn.third);

  do
  {
  printf("Choose an answer: ");
  scanf("%d",&answer);
  if(answer != 1 && answer != 2 && answer != 3)
  printf("Error. Possible answer is 1,2 or 3.\n");
  }
  while(answer != 1 && answer != 2 && answer != 3);

  if(answer == p->qn.snca){
  printf("Answer to the question with serial number %d is correct.\n",sn);
  tncq++;
  fflush(stdin);
  }
  else{
  printf("Answer to the question with serial number %d is not correct.\n",sn);
  fflush(stdin);
  }
  }
  else
  {
  printf("No data about the question with serial number %d.\n",sn);
  fflush(stdin);
  }
  }

  else if(option != '0')
  {
  printf("Unknown option.\n");
  fflush(stdin);
  }

  }
  while(option != '0');

  result=((float)tncq * 100)/tnq;
  printf("Result: %.2f%%",result);

  return 0;
}

How to implement this program using arrays (as given in the original problem)?
 
  • #6
Your code should include the code that was given in this assignment. Your code in post #5 is missing this struct template:
C:
typedef struct
{
    int tnq;//total number of questions
   QUESTION *questions;
}QUIZ;

The questions member is a pointer to a QUESTION struct. With the tnq member, the intent seems to be that you should have an array of type QUESTION rather than a linked list of them. Remember that the name of an array is a pointer to the first element of the array.

When the program starts, you could create a QUIZ instance where questions is the address of an array with two or three elements (your program description doesn't say how many questions the quiz should have when it's first created). To add a question use malloc() to allocate space on the heap for the existing questions (tnq of them) plus one more. Copy the existing questions to the first tnq slots in the allocated memory, and then get the information for the new question and copy it to the last memory slot.

Update tnq to reflect the new question, and update the questions member to point to the allocated memory. The header for malloc() is stdlib.h.

As an alternative, you could use the realloc() function (same header as above) to allocate space for the new question. It will do the copying for you, if necessary.
 
Last edited:
  • #7
@gruba, did any of the suggestions work for you? I've seen several of your threads where you ask a question, and get a number of responses, but never acknowledge them.
 
Last edited:

FAQ: C program -- need an explanation

1. What is a C program and how does it work?

A C program is a collection of instructions written in the C programming language that is used to perform specific tasks. It is a high-level programming language that is compiled into machine code, making it efficient and fast. C programs are executed by a computer in a step-by-step manner, following the instructions written by the programmer.

2. What are the basic components of a C program?

The basic components of a C program include variables, data types, operators, control structures, functions, and arrays. Variables are used to store data, while data types determine the type of data a variable can hold. Operators are used for performing mathematical and logical operations. Control structures alter the flow of the program based on certain conditions. Functions are blocks of code that perform specific tasks, and arrays are used to store a collection of similar data.

3. How is memory managed in a C program?

In a C program, memory is managed through the use of pointers. A pointer is a variable that stores the memory address of another variable. Pointers are used to allocate and deallocate memory dynamically, allowing the program to efficiently use memory as needed.

4. What is the difference between a C program and a C++ program?

C and C++ are both programming languages, but they have some key differences. C is a procedural language, meaning that it follows a step-by-step approach to solving a problem. C++ is an object-oriented language, meaning that it uses objects and classes to structure the code. C++ also has additional features, such as inheritance and polymorphism, which are not present in C.

5. How can I learn C programming?

There are various resources available for learning C programming, including online tutorials, books, and courses. It is recommended to start with the basics, such as understanding the syntax and data types, and then gradually move on to more complex concepts. Practice is key in learning any programming language, so be sure to write and test your own programs to improve your skills.

Similar threads

Replies
2
Views
1K
Replies
7
Views
2K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
1
Views
2K
Replies
3
Views
2K
Replies
7
Views
2K
Back
Top