The code runs perfectly until it prints xx.00.00 which it prints twice. All the other code runs without a problem but each time goes from 23.59.59 -) 00.00.00 it gets printed twice. PLEASE HELP ME
#include <stdio.h>
void check(int number) {
if (number < 10) {
printf("0%d", number);
}
else {
printf("%d", number);
}
}
void count(int present_time, int time_for_alarm) {
int present_hour = present_time/10000;
int present_min = (present_time/100) % 100;
int present_sec = (present_time % 100);
int alarm_hour = time_for_alarm/10000;
int alarm_min = (time_for_alarm/100) % 100;
int alarm_sec = (time_for_alarm % 100);
while (present_hour != alarm_hour) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
if (present_min == 60) {
present_min = 0;
present_hour++;
}
if (present_hour == 24) {
present_hour = 0;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_min != alarm_min) {
if (present_hour == alarm_hour && present_min == alarm_min && present_sec == alarm_sec) {
printf("ALARM");
}
else {
if (present_sec <= 60) {
present_sec++;
if (present_sec == 60) {
present_sec = 0;
present_min++;
}
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
}
}
}
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
}
int main() {
int present_time, time_for_alarm;
printf("What time is it? (HHMMSS)");
scanf("%d", &present_time);
printf("What time should the alarm go off? (HHMMSS)");
scanf("%d", &time_for_alarm);
count(present_time, time_for_alarm);
printf("ALARM");
return 0;
}
Issue is right here:
while (present_sec != alarm_sec) {
check(present_hour);
printf(":");
check(present_min);
printf(":");
check(present_sec);
printf("\n");
present_sec++;
}
In every other loop, you advance present_sec first, then print the time. In this loop, you advance after. When you reach the correct hour and minute, you enter this loop, and you will always duplicate the time here. Try inputting 225555 and 225605. You will see 22:56:00 duplicated.
Related
I have a binary number, and I need to print it as hexdecimal number. (its a function in a bigger project)
I made this function, but I was only able to print it in reverse.
this is the function:
int printOctalToHex (unsigned int octalNum) {
unsigned int binaryNum;
int temp;
int i = 1;
binaryNum = octalToBinary(octalNum);
while (1) {
temp = (binaryNum % 10000);
binaryNum = binaryNum / 10000;
if (temp == 0) {
printf("0");
}
if (temp == 1) {
printf("1");
}
if (temp == 10) {
printf("2");
}
if (temp == 11) {
printf("3");
}
if (temp == 100) {
printf("4");
}
if (temp == 101) {
printf("5");
}
if (temp == 110) {
printf("6");
}
if (temp == 111) {
printf("7");
}
if (temp == 1000) {
printf("8");
}
if (temp ==1001) {
printf("9");
}
if (temp == 1010) {
printf("A");
}
if (temp == 1011) {
printf("B");
}
if (temp == 1100) {
printf("C");
}
if (temp == 1101) {
printf("D");
}
if (temp == 1110) {
printf("E");
}
if (temp == 1111) {
printf("F");
}
if (binaryNum < 1) {
printf("\n");
break;
}
i++;
}
}
Can someone help me with that please? TY!
And if someone has a better way to do it I will be happy to see, because my way is very long.
You need just two functions, one of which is a recursive function:
int f_putchar(char c)
{
write(1, &c, 1);
return 1;
}
void f_hex(int nb)
{
char *hex = "0123456789abcdef";
if (nb < 16)
{
f_putchar(hex[nb]);
}
else
{
f_hex(nb / 16);
f_hex(nb % 16);
}
}
When I run my code, the console doesn't output anything. When I go into "debug as c application mode" and step into the makeBoard() method that is supposed to print stuff nothing is shown on the console. I can't work on this project if the console doesn't work.
Whenever I run my code with only makeBoard() in the int main(void) method, the console outputs what it's supposed to. However, when I add the rest of my code in the int main(void) method, nothing is shown in the console window.
I am very new to C and the eclipse IDE. Do I need to download something?
The makeBoard() method:
void makeBoard(){
printf("Row A: ");
for(int i = 0; i< rowAcounter; i++)
{
printf("O");
}
printf("\n");
printf("Row B: ");
for(int i = 0; i< rowBcounter; i++)
{
printf("O");
}
printf("\n");
printf("Row C: ");
for(int i = 0; i< rowCcounter; i++)
{
printf("O");
}
printf("\n");
}
My complete int main method and the rest of my program:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
void makeBoard();
void nextTurn(int player);
void prompt(int turn);
_Bool isGameOver();
void done(int currentTurn);
void test();
void update();
int rowAcounter = 3;
int rowBcounter = 5;
int rowCcounter = 8;
int currentPlayer = 0; // 0= player 1's turn, 1
= player 2's turn
char firstIn;
char secondIn;
_Bool flag = 0;
int main(void){
makeBoard();
while(isGameOver() == 0){
prompt(currentPlayer);
update();
nextTurn(currentPlayer);
if(flag == 1){
break;
}
makeBoard();
}
done(currentPlayer);
return 0;
}
the rest of my code:
void update(){
poll: scanf(" %c%c", &firstIn, &secondIn);
int checkFirst = firstIn - 'A';
if((checkFirst < 0) || (checkFirst > 2))
{
printf("\n Try again, you ape.");
goto poll;
}
int checkSecond = secondIn - '0';
if((checkSecond < 0 ) || (checkSecond > 8)){
printf("\n Try again, you fricker.");
goto poll;
}
else if(checkFirst == 0){ // the player chose row A
if(checkSecond > 3){
printf("\n Try again, you frick.");
goto poll;
}
else{
rowAcounter = rowAcounter - checkSecond;
}
}
else if(checkFirst == 1){ // the player chose row B
if(checkSecond > 5){
printf("\n Try again, you headass.");
goto poll;
}
else{
rowBcounter = rowBcounter - checkSecond;
}
}
else{ // the player chose row C
if(checkSecond > 8)
{
printf("\n Try again!");
goto poll;
}
else{
rowCcounter = rowCcounter - checkSecond;
}
}
if(isGameOver() == 1){
flag = 1;
}
}
void nextTurn(int player){
if(player == 0){
player = 1;
}else
{
player = 0;
}
}
void prompt(int turn){
if(turn == 0){
printf("Player 1, make your move:");
}
else{
printf("Player 2, make your move:");
}
}
_Bool isGameOver(){
if((rowAcounter == 0) && (rowBcounter == 0) && (rowCcounter == 0)){
return 1;
}
else{
return 0;
}
}
void done(int currentTurn){
if(currentTurn == 0){
puts("Player 1 wins!");
}
else{
puts("Player 2 wins!");
}
}
In the code below there is an error I can't locate causing the computer's selection to not be accounted for. The user's input is being considered in the Nim game yet the computer's pieces are not being subtracted.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
void printInstructions();
int getUserInput(int);
int randomRange(int,int);
int PlayerTurn(int);
int smartCompMove(int);
int dumbCompMove(int);
int main()
{
srand(time(NULL));
int pieceAmount = randomRange(12,24);
char smartCompOrDumb;
printf("Do you want to play a smart computer or a dumb one? Select 'S' or 'D'.");
scanf("%c",&smartCompOrDumb);
bool gameOver = false;
bool playerTurn = true;
printInstructions();
while(pieceAmount > 0 && gameOver == false)
{
if (playerTurn == false)
{
if(pieceAmount <= 4)
{
printf("\nThe Computer has won :P ");
gameOver = true;
exit(0);
}
if (smartCompOrDumb == 's' || smartCompOrDumb == 'S')
{
playerTurn = true;
pieceAmount = smartCompMove(pieceAmount);
printf("%d",pieceAmount);
}
else if (smartCompOrDumb == 'd' || smartCompOrDumb == 'D')
{
playerTurn = true;
pieceAmount = dumbCompMove(pieceAmount);
printf("%d",pieceAmount);
}
}
else
{
if(pieceAmount <= 4)
{
printf("\nYou have won :) ");
gameOver = true;
exit(0);
}
playerTurn = false;
pieceAmount = PlayerTurn(pieceAmount);
printf("%d",pieceAmount);
}
}
return 0;
}
void printInstructions()
{
printf("\nThis game is called Nim and it is thousands of years old.");
printf("\nTake turns picking one to three pieces from a pile and whomever picks the last piece wins.");
printf("\n__________________________________________________________________________________________");
}
int randomRange(int low,int high)
{
return rand()% (high - low) + low;
}
int PlayerTurn(int pieceAmount)
{
pieceAmount = getUserInput(pieceAmount);
return pieceAmount;
}
int getUserInput(int pieceAmount)
{
int userInput = 0;
bool flag = true;
while (flag == true)
{
if (pieceAmount > 4)
{
printf("\nThere are %d pieces remaining.\n",pieceAmount);
printf("\nHow many pieces do you want to select? ");
scanf("%d", &userInput);
if (userInput >= 1 && userInput < 5)
{
pieceAmount = pieceAmount - userInput;
flag = false;
}
else
{
printf("This is not a valid move so try again.");
}
}
}
return pieceAmount;
}
int dumbCompMove(int pieceAmount)
{
int dumbPick = rand() % 3 + 1;
printf("\nComputer will pick from the stack. \n");
pieceAmount = pieceAmount - dumbPick;
printf("\nComputer picked %d pieces. \n", dumbPick );
return pieceAmount;
}
int smartCompMove(int pieceAmount)
{
int smartPick = 1;
printf("\nThe computer will select their pieces. \n");
if (pieceAmount >= 15 && pieceAmount < 24)
{
smartPick = 2;
pieceAmount = pieceAmount - smartPick;
}
else if (pieceAmount >= 10 && pieceAmount < 15)
{
smartPick = 4;
pieceAmount = pieceAmount - smartPick;
}
else if (pieceAmount >= 6 && pieceAmount < 10)
{
smartPick = 1;
pieceAmount = pieceAmount -smartPick;
}
else
pieceAmount = 3;
printf("\nThe computer selected %d pieces. \n",smartPick);
return pieceAmount;
}
I had this code working earlier yet somehow I must have altered something minor and now it will not function properly. I am using the Cloud9 program to run it.
Trying to implement a very simple Roman Numeral to Decimal converter but can't seem to figure out a way for the program to return -1 if any non-roman numeral characters are in the string. This is what I have so far.
#include <stdio.h>
#include <ctype.h>
int convertFromRoman(const char *s)
{
int i = 0;
int total = 0;
while (s[i] != '\0') {
if (isalpha(s[i]) == 0) {
return -1;
}
if (toupper(s[i]) == 'I') {
total += 1;
}
if (toupper(s[i]) == 'V') {
total += 5;
}
if (toupper(s[i]) == 'X') {
total += 10;
}
if (toupper(s[i]) == 'L') {
total += 50;
}
if (toupper(s[i]) == 'C') {
total += 100;
}
if (toupper(s[i]) == 'D') {
total += 500;
}
if (toupper(s[i]) == 'M') {
total += 1000;
} else {
return -1;
}
i++;
}
if (total == 0) {
return -1;
}
return total;
}
int main()
{
printf("%d\n", convertFromRoman("XVII"));
printf("%d\n", convertFromRoman("ABC"));
}
The first one should return 17 and the second one should return -1. However they both return -1 but if I remove the else statement, the first one returns 17 and the second one returns 100.
Any help is appreciated.
Change if() if() if() else to if() else if () else if() else
if (toupper(s[i]) == 'I') {
total += 1;
}
else if (toupper(s[i]) == 'V') {
total += 5;
}
else if (toupper(s[i]) == 'X') {
total += 10;
}
....
else if (toupper(s[i]) == 'M') {
total += 1000;
} else {
return -1;
}
Not really an answer, just a bit of fun/alternate way of looking at the problem. It does solve the problem if you're not considering ordering just adding "digit" values.
char *romanNumerals = "IVXLCDM";
int values[] = { 1, 5, 10, 50, 100, 500, 1000 };
int convertFromRoman(const char *s) {
int val = 0;
for (int i = 0; s[i]; i++) {
char *idx;
if (NULL == (idx = strchr(romanNumerals, toupper(s[i])))) {
return -1;
}
val += values[idx - romanNumerals];
}
return val;
}
When I execute my code, scanf("%d", &n); don't scan anything, I mean, if I introduce any number it doesn't do anything, regardless of the numbers I introduce.
void testEsPrimo() {
int n;
printf("Comprobando si un número es o no primo\n");
printf("Teclee un número entero: ");
fflush(stdout);
scanf("%d", &n); //<---- The problem ?
if(esPrimo(n) == cierto){
printf("%d es primo\n", n);
}else{
printf("%d NO es primo\n", n);
}
fflush(stdout);
}
Logico esPrimo(int n){
int divisor;
int esPrimox;
for(divisor = 2; sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
if(esPrimox == 1) {
return cierto;
}
return falso;
}
This is my esPrimo code that is about decide if a number is prime or not.
typedef enum {falso, cierto} Logico;
and this is Logico, defined in a .h file
PD: This are my first steps on C so my code might be bad.
PD2: Excuse me for my bad English I'm not native and my English isn't really good.
Your scanf is perfect.
I think that your mistake is the loop for from esPrimo. Actually you have an infinite loop, because sqrt(n) has always the same value and it isn't a boolean expression.
Change your loop:
for(divisor = 2; sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
for this:
for(divisor = 2; divisor < sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
But then you have a problem when you know that your number is not prime: you have to finish the loop.
You can do this:
for(divisor = 2; divisor < sqrt(n); divisor++) {
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
break;
}
}
}
But if you can avoid using breakinside a loop for, don't use that.
With complicated algorithms you have a clean code with that, but when you read a loop for, usually your understand that the loop do a exactly number of iterations. If you have another flag to end the loop, use while.
While (divisor < sqrt(n) && esPrimox == 0){
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
} else {
esPrimox =1;
}
}
}
There are 2 main problems in esPrimo.
First, the for loop will not terminate:
for(divisor = 2; sqrt(n); divisor++) {
Change the condition to:
for(divisor = 2; divisor <= sqrt(n); divisor++) {
Second is in the logic. If you find that n is not a prime, you need to break the loop or the function will always return true. You could do it either with the break statement or by checking the value of esPrimox in the loop condition.
Here's how to do it using break:
for(divisor = 2; divisor <= sqrt(n); divisor++) { /* fixed loop condition */
if(n <= 0) {
return falso;
} else {
if(n%divisor == 0) {
esPrimox = 0;
break; /* break the loop */
} else {
esPrimox =1;
}
}
}