Difference between revisions of "Operating Systems 2021F: Assignment 1"

From Soma-notes
Jump to navigation Jump to search
Line 9: Line 9:
==Questions==
==Questions==


# By default, what answers will give you a perfect score in 3000quiz?  Why?
# [2] By default, what answers will give you a perfect score in 3000quiz?  Why?
# How can you change 3000quiz so that if you run "./3000quiz Sally" the answer to question 4 is Sally by changing the data passed to askQuestions()?
# [2] How can you remove the constant numQuestions and replace it with a runtime calculated count of the number of questions?  Does anything in the answers data structure help with this calculation?  Explain briefly.
# How can you change 3000quiz so that if you set the environment variable DOGNAME to Roshi the answer to question 2 will be Roshi?  Again, just change the data passed to askQuestions(), not the code of askQuestions().
# [2] How can you change 3000quiz so that if you run "./3000quiz Sally" the answer to question 4 is Sally?  Only change the data passed to askQuestions(), don't change the code in askQuestions().
# [2] How can you change 3000quiz so that if you set the environment variable DOGNAME to Roshi the answer to question 2 will be Roshi?  Again, just change the data passed to askQuestions(), not the code of askQuestions().


==Code==
==Code==

Revision as of 23:04, 22 September 2021

This assignment is still being developed.

Please submit the answers to the following questions via Brightspace by October 1, 2021. There are ?? points in ?? questions.

Submit your answers as a plain text file following this template. Your answers will be parsed by a script in order to help with grading so please preserve the format of the template. No other formats will be accepted.

Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources. You do not need to list help from the instructor, TA, or information found in the textbook.

Questions

  1. [2] By default, what answers will give you a perfect score in 3000quiz? Why?
  2. [2] How can you remove the constant numQuestions and replace it with a runtime calculated count of the number of questions? Does anything in the answers data structure help with this calculation? Explain briefly.
  3. [2] How can you change 3000quiz so that if you run "./3000quiz Sally" the answer to question 4 is Sally? Only change the data passed to askQuestions(), don't change the code in askQuestions().
  4. [2] How can you change 3000quiz so that if you set the environment variable DOGNAME to Roshi the answer to question 2 will be Roshi? Again, just change the data passed to askQuestions(), not the code of askQuestions().

Code

3000quiz.c

/* 3000quiz.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

char * the_questions[] = {
        "What is the capitol of Canada?",
        "What is the name of Anil's dog?",
        "How many feet in a mile?",
        "What is your name?",
        NULL
};

char * the_answers[] = {
        "Ottawa",
        "ENV: DOGNAME",
        "5280",
        "Bob",
        NULL
};

int askQuestions(char **question, char *answer[],
                 char *argv[], char *envp[])
{
        int score = 0;
        int q = 0;
        int c;
        int res;
        char response[100];
        
        while (question[q] && answer[q]) {
                printf("%s ", question[q]);
                c = scanf("%100s", response);
                if (c != EOF) {
                        if (strcmp(answer[q], response) == 0) {
                                printf("Correct!\n");
                                score++;
                        } else {
                                printf("Sorry, the answer is %s.\n", answer[q]);
                        }
                }
                q++;
        }
        
        return score;
}

int main(int argc, char *argv[], char *envp[])
{
        int score;
        int numQuestions = 4;

        score = askQuestions(the_questions, the_answers, argv, envp);

        printf("\nYour score is %d out of %d.\n", score, numQuestions);

        return 0;
}