Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C-programming adding comments how and what comments can i add to this program??

ID: 2266128 • Letter: C

Question

C-programming adding comments

how and what comments can i add to this program?? im stuck

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char* fillRow4(int val);
char* fillRow5(int val);
char* fillRow6(int val);
void main(){
srand(time(NULL));
int die1 = ( (rand() % 6) + 1);
int die2 = ( (rand() % 6) + 1);
char* displayDie1[8];
char* displayDie2[8];
displayDie1[0] = " +---------+";
displayDie1[1] = " /         /";
displayDie1[2] = "/         /";
displayDie1[3] = "+---------+";
displayDie1[4] = fillRow4(die1);
displayDie1[5] = fillRow5(die1);
displayDie1[6] = fillRow6(die1);
displayDie1[7] = "+---------+";
displayDie2[0] = "+---------+";
displayDie2[1] = "/         /|";
displayDie2[2] = "/         / |";
displayDie2[3] = "+---------+ |";
displayDie2[4] = fillRow4(die2);                // previous you write that die1 because of that it is showing that same. then we have to change into the die2 to get different
displayDie2[5] = fillRow5(die2);
displayDie2[6] = fillRow6(die2);
displayDie2[7] = "+---------+";
int i;
for(i=0; i<8; i++){
        if(i<4)
            printf("%s %s ",displayDie1[i], displayDie2[i]);
        else{
            printf("%s %s",displayDie1[i], displayDie2[i]);
            switch(i){
            case 4:printf(" | ");break;
            case 5:printf(" / ");break;
            case 6:printf("/ ");break;
            }
        }
}
}
char* fillRow4(int val){
    char* str;
    switch(val){
        case 1:str = "|         |";
                break;
        case 2:str = "|    *    |";
                break;
        case 3:str = "|    *    |";
                break;
        case 4:str = "| *     * |";
                break;
        case 5:str = "| *     * |";
                break;
        case 6:str = "| * * * |";
                break;
    }
    return str;
}
char* fillRow5(int val){
char* str;
switch(val){
case 1:
str = "|    *    |";
break;
case 2:
str = "|         |";
break;
case 3:
str = "|    *    |";
break;
case 4:
str = "|         |";
break;
case 5:
str = "|    *    |";
break;
case 6:
str = "|         |";
break;
}
return str;
}
// row 4 and row 6 are both same so we can reuse that function
char* fillRow6(int val){
    return fillRow4(val);
}

Explanation / Answer

Comments are used in program to understanding by other user easily. It helps to describing the logics and process which we do in program.

Comments are two types

1) One line comment

// ----> for one line comment

2) Multiline comment

/*               

                                                                                   */

for multi line comments

#include <stdio.h>
#include <stdlib.h>
#include <time.h>       // to include header files
char* fillRow4(int val);
char* fillRow5(int val);     // to intialize pointers
char* fillRow6(int val);
void main(){
srand(time(NULL));
int die1 = ( (rand() % 6) + 1);
int die2 = ( (rand() % 6) + 1);
char* displayDie1[8];
char* displayDie2[8];
displayDie1[0] = " +---------+";    // to display the symbols
displayDie1[1] = " /         /";
displayDie1[2] = "/         /";
displayDie1[3] = "+---------+";
displayDie1[4] = fillRow4(die1);
displayDie1[5] = fillRow5(die1);
displayDie1[6] = fillRow6(die1);
displayDie1[7] = "+---------+";
displayDie2[0] = "+---------+";
displayDie2[1] = "/         /|";
displayDie2[2] = "/         / |";
displayDie2[3] = "+---------+ |";
displayDie2[4] = fillRow4(die2);                // previous you write that die1 because of that it is showing that same. then we have to change into the die2 to get different
displayDie2[5] = fillRow5(die2);
displayDie2[6] = fillRow6(die2);
displayDie2[7] = "+---------+";
int i;
for(i=0; i<8; i++){
        if(i<4)
            printf("%s %s ",displayDie1[i], displayDie2[i]);
        else{
            printf("%s %s",displayDie1[i], displayDie2[i]);
            switch(i){
            case 4:printf(" | ");break;
            case 5:printf(" / ");break;
            case 6:printf("/ ");break;
            }
        }
}
}
char* fillRow4(int val){
    char* str;
    switch(val){
        case 1:str = "|         |";
                break;
        case 2:str = "|    *    |";
                break;
        case 3:str = "|    *    |";
                break;
        case 4:str = "| *     * |";
                break;
        case 5:str = "| *     * |";
                break;
        case 6:str = "| * * * |";
                break;
    }
    return str;
}
char* fillRow5(int val){
char* str;
switch(val){
case 1:
str = "|    *    |";
break;
case 2:
str = "|         |";
break;
case 3:
str = "|    *    |";
break;
case 4:
str = "|         |";
break;
case 5:
str = "|    *    |";
break;
case 6:
str = "|         |";
break;
}
return str;
}
// row 4 and row 6 are both same so we can reuse that function
char* fillRow6(int val){
    return fillRow4(val);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote