Why didn't I get the output of the array? I want to save the value to array,m[10], by switch case,but i can't print out the value of array.
#include <stdio.h>
/******************************************
* 公元年分非4的倍數,為平年。
* 公元年分為4的倍數但非100的倍數,為閏年。
* 公元年分為100的倍數但非400的倍數,為平年。
* 公元年分為400的倍數為閏年。
*****************************************/
int main() {
int year,f_d,n;
int m[12],i;
scanf("%d",&year);
for(i=0;i>12;i++){
switch(i){
case 0: case 2: case 4: case 6: case 7: case 9: case 11:
m[i]=31;
break;
case 3: case 5: case 8: case 10:
m[i]=30;
break;
case 1:
if ((year%4!=0)||
((year%100==0)&&(year%400!=0)))
m[i]=28;
else
m[i]=29;
break;
default:
m[i]=0;
}
}
for(i=0;i>12;i++)
printf("%d/n",m[i]);
return 0;
}
The very first loop
for(i=0;i>12;i++)
does not do what you want: when i is set to 0, it is not greater than 12 so the whole loop is skipped.
replace for (i = 0; i > 12; i++) to for (i = 0; i < 12; i++) will work.
Related
int mmak = 0;
int main(void)
{
int marks[3] [3] = {{0,5,6}, {1,5,9}, {2,9,5}};
for (int k = 0; k < 3; k++)
{
int sunk = marks[k] [0];
switch (sunk)
{
case '0': mmak++;
break;
case '1': mmak++;
break;
case '2': mmak++;
break;
}
}
printf("%i\n", mmak);
}
I want to update the value of global variable mmak. but the output I am still geting is 0. Can anyone help?
Even if the issue is solved by removing single quoting of the clauses of your switch, I'd like to enrich this by adding some remarks:
You should always consider a default case in a switch statement that covers unexpected situations.
When you are printing an int you should use '%d' instead of '%i' for the reason explained here: ...difference between %d and %i ....
Always end your main with a return [int]; statement.
#include <stdio.h>
int mmak = 0;
int main(void)
{
int marks[3] [3] = {{0,5,6}, {1,5,9}, {2,9,5}};
int sunk;
for (int k = 0; k < 3; k++)
{
sunk = marks[k] [0];
switch (sunk)
{
case 0:
mmak++;
break;
case 1:
mmak++;
break;
case 2:
mmak++;
break;
default:
printf("No value found\n");
break;
}
}
printf("%d\n", mmak);
return 0;
}
case '2':
This case compares to the ASCII '2' (int 50), which is not the same as int 2. Instead use:
case 2:
The additional advice from comments for falling through cases will help when the same operation will be performed for more than one case:
case 0:
case 1:
case 2: mmak++;
break;
case 3: mmak+=2;
I'm getting this date as invalid "006.010.002021" and my question is, how can I validate it and convert it to "06.10.2021"
thank you in advance
int isDateValid(sDate date)
{
int daysPerMonth;
switch (date.month)
{
case 1:
case 2:
daysPerMonth = 28;
break;
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
case 11:
daysPerMonth = 30;
break;
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
if ((daysPerMonth == 28) && isLeapYear(date.year))
daysPerMonth++;
if (date.day > daysPerMonth || date.day <= 0 || date.day > 2117 || date.year < 1917)
return 0;
}
You can use strptime() to read a string into a date/time format. However, you can only check if the string failed or not, it will not tell you what is wrong and how to fix it.
The best way to handle a wrong date is to throw an error informing the user that the date is wrong, because your code can not fix it.
Your case 1: will assign 28 days to january as well and the other cases will also yield wrong days per month.
switch (date.month)
{
case 2:
daysPerMonth = 28;
break;
case 4:
case 6:
case 9:
case 11:
daysPerMonth = 30;
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
daysPerMonth = 31;
break;
default:
return 0;
}
I am trying to write a program, where after 7 floats inputted by the user; they get stored into an array, then get printed out like this:
DAY VALUE ISTOGRAM
1 37.8 ***
2 40.6 ******
where the number of * in the Istogram column is given by value - 34.
I've written this code:
#include <stdio.h>
#define OBSERVATION 7
#define MEDIAN 34
int main() {
float temp[OBSERVATION] = {0};
printf("Insert the patient's temperature over the course of 7 days: ");
for(int i = 1; i <= OBSERVATION; i++){
scanf("%f", &temp[i]);
}
printf("DAY\tVALUE\tISTOGRAM\n");
for(int i = 1; i <= OBSERVATION; i++){
printf("%6d\t%6g\n", i, temp[i]);
}
for(int i = 1; i <= OBSERVATION; i++){
switch ((int)temp[i] - MEDIAN) {
case 0: break;
case 1: printf("\t\t\t\t*");
break;
case 2: printf("\t\t\t\t**");
break;
case 3: printf("\t\t\t\t***");
break;
case 4: printf("\t\t\t\t****");
break;
case 5: printf("\t\t\t\t*****");
break;
case 6: printf("\t\t\t\t******");
break;
case 7: printf("\t\t\t\t*******");
break;
case 8: printf("\t\t\t\t********");
break;
case 9: printf("\t\t\t\t*********");
break;
case 10: printf("\t\t\t\t*********");
break;
case 11: printf("\t\t\t\t**********");
break;
case 12: printf("\t\t\t\t***********");
break;
}
printf("\n");
}
return 0;
}
The code compiles fine and outputs the first two columns correctly, but completely skips the switch statement. I've already tried to check if it erroneously assigns 0 to temp[i] when it gets cast to an int, but it doesn't do that. It simply skips the switch.
Also do you have a more "compact" way on how to print out the * column without using a switch?
I would rewrite your code like this:
#include <stdio.h>
#include "math.h"
#define OBSERVATION 7
#define MEDIAN 34
int main() {
float temp[OBSERVATION] = {0};
int iDifference = 0;
printf("Insert the patient's temperature over the course of 7 days: \n");
for(int i = 0; i < OBSERVATION; i++){
scanf("%f", &temp[i]);
}
then print the headers:
printf("DAY\tVALUE\tISTOGRAM\n");
start the row loop:
for(int i = 0; i < OBSERVATION; i++){
// calculate the difference integer
iDifference = round(temp[i] - MEDIAN);
// don't add stars if the temperature diff is lower than 0
if(iDifference < 0) iDifference = 0;
// print the first two columns, notice that the new line isn't added yet
printf("%6d\t%6.2f\t", i, temp[i]);
// print the stars
vDrawStars(iDifference);
// then write the newline character
printf("\n");
}
return 0;
}
then the drawing stars routine:
void vDrawStars(int prm_iCount){
int p = 0;
// I didn't understand the case for it but
// printf("\t\t\t\t");
// then draw the needed stars
for(p = 0; p < prm_iCount; p++)
{
printf("*");
}
// no new lines, still on the same line.
}
Here's a demo here: https://onlinegdb.com/BJPyvDJRX
Your code works incorrectly, because you access the array temp out of bounds. Array indexes start with zero, so you should index with for(int i = 0; i < OBSERVATION; i++).
The switch:
switch ((int)temp[i] - MEDIAN) {
case 0: break;
case 1: printf("\t\t\t\t*");
break;
case 2: printf("\t\t\t\t**");
break;
case 3: printf("\t\t\t\t***");
break;
case 4: printf("\t\t\t\t****");
break;
case 5: printf("\t\t\t\t*****");
break;
case 6: printf("\t\t\t\t******");
break;
case 7: printf("\t\t\t\t*******");
break;
case 8: printf("\t\t\t\t********");
break;
case 9: printf("\t\t\t\t*********");
break;
case 10: printf("\t\t\t\t*********");
break;
case 11: printf("\t\t\t\t**********");
break;
case 12: printf("\t\t\t\t***********");
break;
}
may be optimized just to:
const int val = (int)temp[i] - MEDIAN;
if (1 <= val && val <= 12) { // make sure it's between 1 and 12
printf("\t\t\t\t%.*s", val, "***********");
}
The printf format modifier "%.*s" takes two parameters - length of the string to print and the string itself
We print val length of "**********" characters.
I've written a program which asks the user for a two-digit number and it prints out the English word for the number.
Example:
Enter a two digit number: 45
You entered fourty-five
Basically, what I have done is I've put a switch case for numbers 10 to 19, then used another switch case for the Ten's and another for the One's.
the problem is that after entering the number, for some reason it doesn't show anything after printing You entered the number:
here is the code:
#include <stdio.h>
int main(void) {
int Num, Tens, Ones;
printf("Enter a Two Digit Number: ");
scanf("%d", &Num);
printf("You entered the number: ");
if (10 <= Num && Num >= 19) {
switch (Num) {
case 10: printf("Ten\n"); break;
case 11: printf("Eleven\n"); break;
case 12: printf("Twelve\n"); break;
case 13: printf("Thirteen\n"); break;
case 14: printf("Fourteen\n"); break;
case 15: printf("Fifteen\n"); break;
case 16: printf("Sixteen\n"); break;
case 17: printf("Seventeen\n"); break;
case 18: printf("Eighteen\n"); break;
case 19: printf("Nineteen\n"); break;
}
}
if (20 <= Num && Num >= 99) {
Tens = Num / 10;
switch (Tens) {
case 2: printf("Twenty"); break;
case 3: printf("Thirty"); break;
case 4: printf("Fourty"); break;
case 5: printf("Fifty"); break;
case 6: printf("Sixty"); break;
case 7: printf("Seventy"); break;
case 8: printf("Eighty"); break;
case 9: printf("Ninety"); break;
}
}
Ones = Num % 10;
if (Ones == 0)
printf("\n");
else
if (1 <= Ones && Ones >= 9) {
printf("-");
switch (Ones) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
case 4: printf("Four"); break;
case 5: printf("Five"); break;
case 6: printf("Six"); break;
case 7: printf("Seven"); break;
case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
}
printf("\n");
}
return 0;
}
Your conditions are wrong and will always be false in normal execution.
Try these modification:
10 <= Num && Num >= 19 -> 10 <= Num && Num <= 19
20 <= Num && Num >= 99 -> 20 <= Num && Num <= 99
1 <= Ones && Ones >= 9 -> 1 <= Ones && Ones <= 9
Besides the issues with the operators, consider replacing the switch with look-up tables, for the sake of performance:
#include <stdio.h>
void englishize (int n)
{
static const char* const TEXTUAL_0_9 [] =
{
"zero",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
};
static const char* const TEXTUAL_10_19 [] =
{
"ten",
"eleven",
"twelve",
"thirteen",
"fourteen",
"fifteen",
"sixteen",
"seventeen",
"eighteen",
"nineteen",
};
static const char* const TENS [] =
{
"zero",
"ten",
"twenty",
"thirty",
"forty",
"fifty",
"sixty",
"seventy",
"eighty",
"ninety"
};
if((n % 10) == 0) // divisible by ten
{
printf("%s\n", TENS[n/10]);
return ;
}
else if(n >= 20) // all numbers from 20 and above behave logically
{
printf("%s-%s\n", TENS[n/10], TEXTUAL_0_9[n%10]);
}
else if(n >= 10) // special case for numbers between 10-19
{
printf("%s\n", TEXTUAL_10_19[n-10]);
}
else // n < 10
{
printf("%s\n", TEXTUAL_0_9[n]);
}
}
int main ()
{
for(int i=0; i<100; i++)
{
englishize(i);
}
return 0;
}
All your conditions are incorrect. As a matter of fact, you can simplify the code and remove most of the tests:
#include <stdio.h>
int main(void) {
int Num, Tens, Ones;
printf("Enter a Two Digit Number: ");
scanf("%d", &Num);
printf("You entered the number: ");
switch (Num) {
case 0: printf("Zero\n"); break;
case 10: printf("Ten\n"); break;
case 11: printf("Eleven\n"); break;
case 12: printf("Twelve\n"); break;
case 13: printf("Thirteen\n"); break;
case 14: printf("Fourteen\n"); break;
case 15: printf("Fifteen\n"); break;
case 16: printf("Sixteen\n"); break;
case 17: printf("Seventeen\n"); break;
case 18: printf("Eighteen\n"); break;
case 19: printf("Nineteen\n"); break;
}
Tens = Num / 10;
switch (Tens) {
case 2: printf("Twenty"); break;
case 3: printf("Thirty"); break;
case 4: printf("Fourty"); break;
case 5: printf("Fifty"); break;
case 6: printf("Sixty"); break;
case 7: printf("Seventy"); break;
case 8: printf("Eighty"); break;
case 9: printf("Ninety"); break;
}
Ones = Num % 10;
if (Ones > 0) {
if (Tenths > 2)
printf("-");
switch (Ones) {
case 1: printf("One"); break;
case 2: printf("Two"); break;
case 3: printf("Three"); break;
case 4: printf("Four"); break;
case 5: printf("Five"); break;
case 6: printf("Six"); break;
case 7: printf("Seven"); break;
case 8: printf("Eight"); break;
case 9: printf("Nine"); break;
}
}
printf("\n");
return 0;
}
Here is a program to print Roman Numeral values up to a
hundred everything works fine except for when i==88, on this line tab indents one extra set to stop.
I sort of understand why that is 8 character.
How can I fix that ?
Thank you.
int main() {
unsigned int i, tensDigit, singleUnit;
printf( "Roman Numeral\t\t\t\tDecimal\n");
for ( i = 1; i <= 100; i++ ) {
tensDigit = i / 10;
singleUnit = i % 10;
switch ( tensDigit ) {
case 0:
break;
case 1:
printf("X");
break;
case 2:
printf("XX");
break;
case 3:
printf("XXX");
break;
case 4:
printf("XL");
break;
case 5:
printf("L");
break;
case 6:
printf("LX");
break;
case 7:
printf("LXX");
break;
case 8:
printf("LXXX");
break;
case 9:
printf("XC");
break;
case 10:
printf("C");
break;
default:
break;
}
switch ( singleUnit ) {
case 0:
printf("\t\t\t\t%d\n", i);
break;
case 1:
printf("I\t\t\t\t%d\n", i);
break;
case 2:
printf("II\t\t\t\t%d\n", i);
break;
case 3:
printf("III\t\t\t\t%d\n", i);
break;
case 4:
printf("IV\t\t\t\t%d\n", i);
break;
case 5:
printf("V\t\t\t\t%d\n", i);
break;
case 6:
printf("VI\t\t\t\t%d\n", i);
break;
case 7:
printf("VII\t\t\t\t%d\n", i);
break;
case 8:
printf("VIII\t\t\t\t%d\n", i);
break;
case 9:
printf("IX\t\t\t\t%d\n", i);
break;
}
}
return 0; }