Objective: Trasfer the follwing C++ code to C. So I have this code that is C++,
ID: 3771374 • Letter: O
Question
Objective: Trasfer the follwing C++ code to C.
So I have this code that is C++, but I need it in C. A lot of it is just making "cout" to "printf", but some of it I do not know how to make it C. I also know that "#include " needs to be changed to "#include "
Here is the C++ code that needs to be made into just C:
-----------------
#include <iostream>
#include <time.h>
using namespace std;
void rollDies(int dice[], char choise[]);
void display(int dice[]);
void display(int category, int value);
void readChoise(char choise[]);
int main()
{
srand(time(NULL));
int dice[5];
int categories[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int category;
int bonus = 0;
char ch = 'y';
do{
char choise[5] = {'y','y','y','y','y'};
rollDies(dice,choise);
cout<<"your first roll is: ";
display(dice);
cout<<". (Type 'y' to re-roll or 'n' to stay)"<<endl;
readChoise(choise);
rollDies(dice,choise);
cout<<"your second roll is: ";
display(dice);
cout<<". (Type 'y' to re-roll or 'n' to stay)"<<endl;
readChoise(choise);
rollDies(dice,choise);
cout<<"your third roll is: ";
display(dice);
cout<<" Under what category would you like to record your score: ";
cout<<" 1 - Ones, 2 - Twos, 3 - Threes,";
cout<<" 4 - Fours, 5 - Fives, 6 - Sixes,";
cout<<" 7 - Three of a Kind, 8 - Four of a Kind, 9 - Full House,";
cout<<" 10 - Small Straight, 11 - Large Straight, 12 - Yahtzee,";
cout<<" 13 - Chance, 14 - Scratch"<<endl;
cout<<" Choice (1-14): ";
cin>>category;
categories[category-1] = categories[category-1] +1;
for(int i=0;i<14;i++)
display(i,categories[i]);
int score_1_6 = 0;
for(int i=0;i<6;i++)
score_1_6 += categories[i];
if(score_1_6 >= 63)
bonus = 50;
int Yahtzee = categories[11] ;
if(Yahtzee > 0)
bonus += 50 + (Yahtzee - 1)*100;
cout<<"Bonus: "<<bonus<<endl;
cout<<"Do you want to play again: y or n : ";
cin>>ch;
}while(ch != 'n' && ch!= 'N');
return 0;
}
void rollDies(int dice[], char choise[])
{
for(int i=0;i<5;i++)
if(choise[i] == 'y')
dice[i] = (rand() % 6) +1;
}
void display(int dice[])
{
for(int i=0;i<5;i++)
cout<<dice[i]<<" ";
}
void display(int category, int value)
{
cout<<category<<" - ";
switch(category + 1)
{
case 1: cout<<"Ones"; break;
case 2: cout<<"Twos"; break;
case 3: cout<<"Threes"; break;
case 4: cout<<"Fours"; break;
case 5: cout<<"Fives"; break;
case 6: cout<<"Sixes"; break;
case 7: cout<<"Three of kind"; break;
case 8: cout<<"Four of Kind"; break;
case 9: cout<<"Full House"; break;
case 10: cout<<"Sm. Straight"; break;
case 11: cout<<"Lg. Straight"; break;
case 12: cout<<"Yahtzee"; break;
case 13: cout<<"Chance"; break;
case 14: cout<<"Scratch"; break;
}
cout<<" "<<value<<endl;
}
void readChoise(char choise[])
{
for(int i=0;i<5;i++)
cin>>choise[i];
}
Explanation / Answer
C Code:
#include<time.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void rollDies(int dice[], char choise[]);
void display_dice(int dice[]);
void display(int category, int value);
void readChoise(char choise[]);
int main()
{
int my;
int dice[5];
int categories[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int category;
int bonus = 0;
int i;
char ch = 'y';
int score_1_6 = 0;
int Yahtzee;
clrscr();
srand(time(NULL));
do{
char choise[5] = {'y','y','y','y','y'};
rollDies(dice,choise);
printf("your first roll is: ");
display_dice(dice);
printf(". (Type 'y' to re-roll or 'n' to stay )");
readChoise(choise);
rollDies(dice,choise);
printf("your second roll is: ");
display_dice(dice);
printf(". (Type 'y' to re-roll or 'n' to stay )");
readChoise(choise);
rollDies(dice,choise);
printf("your third roll is: ");
display_dice(dice);
printf(" Under what category would you like to record your score: ");
printf(" 1 - Ones, 2 - Twos, 3 - Threes,");
printf(" 4 - Fours, 5 - Fives, 6 - Sixes,");
printf(" 7 - Three of a Kind, 8 - Four of a Kind, 9 - Full House,");
printf(" 10 - Small Straight, 11 - Large Straight, 12 - Yahtzee,");
printf(" 13 - Chance, 14 - Scratch ");
printf(" Choice (1-14): ");
fflush(stdin);
scanf("%d",&category);
categories[category-1] = categories[category-1] +1;
for(i=0;i<14;i++)
display(i,categories[i]);
for(i=0;i<6;i++)
score_1_6 += categories[i];
if(score_1_6 >= 63)
bonus = 50;
Yahtzee = categories[11] ;
if(Yahtzee > 0)
bonus += 50 + (Yahtzee - 1)*100;
printf("Bonus: %d ", bonus);
printf("Do you want to play again: y or n : ");
fflush(stdin);
ch=getchar();
}while(ch != 'n' && ch!= 'N');
return 0;
}
void rollDies(int dice[], char choise[])
{
int i;
for(i=0;i<5;i++)
if(choise[i] == 'y')
dice[i] = (rand() % 6) +1;
}
void display_dice(int dice[])
{
int i;
for(i=0;i<5;i++)
printf("%d ",dice[i]);
}
void display(int category, int value)
{
printf("%d -",category);
switch(category + 1)
{
case 1: printf("Ones"); break;
case 2: printf("Twos"); break;
case 3: printf("Threes"); break;
case 4: printf("Fours"); break;
case 5: printf("Fives"); break;
case 6: printf("Sixes"); break;
case 7: printf("Three of kind"); break;
case 8: printf("Four of Kind"); break;
case 9: printf("Full House"); break;
case 10: printf("Sm. Straight"); break;
case 11: printf("Lg. Straight"); break;
case 12: printf("Yahtzee"); break;
case 13: printf("Chance"); break;
case 14: printf("Scratch"); break;
}
printf(" %d ",value);
}
void readChoise(char choise[])
{
int i;
for(i=0;i<5;i++)
scanf("%c",choise[i]);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.