I'm getting better in my C coding but I'm still unsure how to solve several problems in my code. The professor wants us to write a code where we input the day (mm/dd/yyyy) and spits out the number of days that have passed. For choice 2, input number of days in a certain year and spits out the corresponding date. The problem is (since our prof likes to mess with us) solving for errors like 4/31/2001 where April doesn't have 31 days and inputing 366 days in a non-leap year.
int leap(int x)
{
if ((x % 4 == 0 && x % 100 != 0)|| x % 400 == 0)
return 1;
else
return 0;
}
int monthFun(int d, int y)
{
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int i;
if (leap(y))
days[1] = 29;
for (i = 0; ; i++)
if (d > days[i])
d = d - days[i];
else
return i + 1;
return i + 1;
}
int dateFun(int d, int y)
{
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int i, m;
if (leap(y))
days[1] = 29;
for (i = 0; i < m - 1; i++)
if (d > days[i]) {
d = d - days[i];
}
else {
d = d;
m = i;
}
return d;
}
int daysPast(int m, int d, int y)
{
int days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
int i, past = 0;
if (leap(y))
days[1] = 29;
for (i = 0; i < m - 1; i++)
past = past + days[i];
return past + d;
}
int main()
{
int mm, dd, yy, choice;
char more;
do {
printf ("\n\t\tThis program will find days passed or date in the year");
printf ("\n\t\t\t\t1) Input date (mm/dd/yyyy) to find the days passed");
printf ("\n\t\t\t\t2) Input passed days to find date in the year");
printf ("\n\n\t\t\t\tYour choice (1/2): ");
scanf ("%d", &choice);
if (choice == 1) {
printf ("\n\t\tPlease input date (mm/dd/yyyy): ");
scanf ("%d/%d/%d", &mm, &dd, &yy);
printf ("\n\t\tThere are %d days passed in the year %d", daysPast(mm, dd, yy), yy);
}
else
if (choice == 2){
printf ("\n\t\tInput days: ");
scanf ("%d", &dd);
printf ("\n\t\tInput year: ");
scanf ("%d", &yy);
printf ("\n\t\tThe date is %d/%d/%d", monthFun(dd, yy), dateFun(dd,yy), yy);
}
else
printf ("\n\t\tPlease choose 1 or 2");
printf ("\n\n\t\tDo more (Y/N)? ");
scanf ("%s", &more);
} while (more == 'y' || more == 'Y');
}
The code for my dateFun() is a little sloppy (or at least seems like it to me). I figured that if one of the aforementioned problems is entered, the program says something like "invalid input" and takes you back to "do more". Any tips are appreciated and thanks in advance!
Your dateFun tests an uninitialized m.
dateFun() and monthFun() are looking very similar, I've added a sample combined version.
int leap(int x)
{
return ((x % 4 == 0 && x % 100 != 0)|| x % 400 == 0);
}
int monthFun(int d, int y)
{
int i, days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
days[1] += leap(y);
for (i=0 ; d>days[i] ; i++)
d -= days[i];
return i+1;
}
int dateFun(int d, int y)
{
int i, days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
days[1] += leap(y);
for (i=0 ; d>days[i] ; i++)
d -= days[i];
return d;
}
int datemonthFun(int d, int y)
{
int i, days[] = {31,28,31,30,31,30,31,31,30,31,30,31};
days[1] += leap(y);
for (i=0 ; d>days[i] ; i++)
d -= days[i];
return (i+1)*100+d;
}
Related
I'm trying to build a program that will print out the month and year that the user inputs. What I have so far ends up with the year having 5 digits and inputting "2020 2" results in 28 days for the month of February.
The code I have so far in this assignment:
#include <stdio.h>
#include<stdlib.h>
int is_leapyear(int y)
{
if ((y % 100 == 0 && y % 100 != 0) || y % 400 == 0)
return 1;
else
return 0;
}
int main()
{
int i, j, year, month;
printf("Please enter the year and month:");
scanf("%d", &year);
scanf("%d", &month);
int a[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
int la[13] = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
int c = 1;
int flag = 0;
if (is_leapyear(year))
flag = 1;
for (i = 0; i<month; i++)
{
if (flag == 1)
c += la[i];
else
c += a[i];
}
int s = (year - 1) + ((year - 1) / 4) + ((year - 1) / 400) - ((year - 1) / 100) + c;
int week = s % 7;
printf("Calendar " );
printf("%-d", year);
if (month < 10)
printf("0");
printf(" %d\n", month);
printf("----------------------\n");
printf("Su Mo Tu We Th Fr Sa \n");
printf("----------------------\n");
if (flag == 1) {
for (j = 0; j < week; j++)
{
printf(" ");
}
for (i = 1; i <= la[month]; i++)
{
if ((week + i - 1) % 7 == 0)
{
printf("\n");
}
if (i < 10)
{
printf(" ");
}
printf("%d", i);
printf(" ");
}
}
else {
for (j = 0; j < week; j++)
{
printf(" ");
}
for (i = 1; i <= a[month]; i++)
{
if ((week + i - 1) % 7 == 0)
{
printf("\n");
}
if (i < 10)
{
printf(" ");
}
printf("%d", i);
printf(" ");
}
}
printf("\n");
printf("---------------------\n");
}
The output:
Any help would be greatly appreciated.
Edit: It seems like I was too careless in reviewing my code, minor mistakes observed and problem solved!
The century check on your isleapyear()
if ((y % 100 == 0 && y % 100 != 0) || y % 400 == 0)
is flawed. It checked if the year is divisible by 100 and not divisible by 100, which is a logical impossibility. I think you actually mean
if ((y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
So check if the year is divisible by 4 and not divisible by 100.
As for the 20200, it's because your leading zero for the month is printed before the space is printed. Just change it to
printf("%-d", year);
printf(" "); //put space right away
if (month < 10)
printf("0");
printf("%d\n", month); //no need to print space here
or even better as pointed out by #JonathanLeffler in the comment
printf(" %.2d\n", month);
It deals with space, the leading zero, and the new line in just one call, or just
printf("Calendar %.4d %.2d\n", year, month);
to handle those three parts at once
OP's is_leapyear(int y) fails with various multiples of 4.
Instead:
// Alternative for Gregorian calendar
int is_leapyear(int y) {
if (y % 4 == 0) {
if (y % 100 == 0) { // A century year?
return (y % 400 == 0);
}
return 1;
}
return 0;
}
Deeper: Leap year.
This question already has answers here:
Variable leading zeroes in C99 printf
(1 answer)
printf string, variable length item
(2 answers)
Closed 5 years ago.
I've written a programm for college where I print out all prime number twins between two numbers. (f.e. between 1 and 12000)
In my printing statement i've written %04d for 4 digits. But what i want to do is to make this variable. (I tried %0%dd, but this didnt work.) I dont want to do write just the max digit count of int. I count hte digits of the int with int count = floor(log10(abs(b))) + 1;
Heres my complete code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(int a);
void listOfPrimeNumberTwins(int a, int b);
typedef int twins[10000][2];
int main(){
int a;
int b;
printf("Check if prime:\nEnter number: ");
scanf(" %d", &a);
b = isPrime(a);
if (b == 0){
printf("No Prime!");
} else if (b == -1){
printf("Negative number!");
} else {
printf("Prime!");
}
printf("\nPrime Number Twins:\nEnter number 1: ");
scanf(" %d", &a);
printf("Enter number 2: ");
scanf(" %d", &b);
listOfPrimeNumberTwins(a,b);
}
int isPrime(int a){
int i;
int b = 0;
if (a == 1){
return 0;
}
if (a <= 0){
return -1;
}
for (i = 2; i < a; i++){
if (a % i == 0){
return 0;
}
}
return 1;
}
void listOfPrimeNumberTwins(int a, int b){
int count = floor(log10(abs(b))) + 1;
int i;
int j = 0;
twins c;
b -= 1;
for (i = a; i < b; i++){
if (i > 1 && isPrime(i) == 1 && isPrime(i + 2) == 1){
c[j][0] = i;
c[j][1] = i + 2;
j += 1;
}
}
if (j == 0){
printf("No Prime number twins between %d and %d!", a,b);
} else {
printf("Prime number twins between %d and %d:\n", a,b);
for (i = 0;i < j; i++){
printf("%04d\t<-->\t%04d\n", c[i][0],c[i][1]);
}
}
}
How can I achieve what I want? Or is it just impossible like I want it?
I would like to get an output of the biggest even number. but when I input 1 2 3 (3 calls to scanf) the output is 4.
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Before the loop initialize ary[0] for example the following way (otherwise uninitialized value of ary[0] is used in the program)
ary[0] = 1;
then substitute these if statements
if(ary[x]%2==0)
{
if(ary[0]<ary[x])
for
if( ary[x]%2==0 && ( x == 1 || ary[0]<ary[x] ) )
And at last write
if ( ary[0] != 1 ) printf("%d",ary[0]);
Take into account that this call
fflush(stdin);
has undefined behavior and should be removed.
In fact there is no need to declare an array. Without the array the program can look like
#include <stdio.h>
int main( void )
{
unsigned int n;
int max_even = 1;
printf("How many numbers are you going to enter: ");
scanf("%u", &n);
int x;
for (unsigned int i = 0; i < n && scanf( "%d", &x ) == 1; i++)
{
if ((x % 2) == 0 && (max_even == 1 || max_even < x))
{
max_even = x;
}
}
if (max_even != 1)
{
printf("maximum entered even number is %d\n", max_even);
}
else
{
puts("None even number was enetered");
}
return 0;
}
Its output might look like
How many numbers are you going to enter: 10
0 1 2 3 4 5 6 7 8 9
maximum entered even number is 8
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int ary[0 = 0;
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Your code does not work because ary[0] is not yet initialized the first time you compare its value to the value read, furthermore it might not be even for the other comparisons.
You should use an indicator telling you whether an even value has been seen.
Here is a solution:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int has_even = 0, max_even = 0, value, amount, x;
if (scanf("%d", &amount) != 1)
return 1;
for (x = 0; x < amount; x++) {
if (scanf("%d", &value) != 1)
break;
if (!has_even || value > max) {
max_even = value;
has_even = 1;
}
}
if (has_even)
printf("%d\n", max_even);
else
printf("no even value\n");
getchar();
return 0;
}
This code is used to get n dates and sort them in the ascending order. I think the getdates function is not working properly.
main function implementation:
#include <stdio.h>
#define max 30
int leapyear(int year);/*to check whether it's leap year or not*/
int dater(int x);/*to find the date from a month begin with the beginning of the year */
void getdates(int f);/*get the date in put*/
int *caldays(int p);
int n, i, q;
int t, d, leap;
int day[30];
int month[30];
int year[30];
char ca[30];
char cb[30];
int dd[30];
int da[30];
int j, a, x;
char c1, c2;
int dayn, monthn, yearn;
int main()
{
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
printf("end");
for (i = 0; i <= n-1; i++)
{
printf("end");
while (day[i] < 1 || day[i] > 31 || month[i] < 1 ||
month[i] > 12 || year[i] < 1 || year[i] > 10000)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
while (month[i] == 2 && day[i] > 29)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
while ((month[i] == 4 ||month[i] == 6 ||
month[i] == 9 ||month[i] == 11) && day[i] > 30)
{
fprintf( stderr, "The date entered is not right, please enter again\n");
printf("Please enter the number of dates and Press ENTER to finish\n");
/*get the numbers of dates*/
scanf("%i", &n);
printf("You have entered %i \n", n);
getdates(n);
}
}
/*3 while loops are used to give msg and re-enter again when get an error input*/
caldays(n);
for (i = 0; x < n; ++i)
{
for (j = i + 1; j < n; ++j)
{
if (dd[i] > dd[j])
{
a = dd[i];
dd[i] = dd[j];
dd[j] = a;
}
/*sort the days in asending order in days array*/
}
}
printf("The %i dates in ascending order are\n", n);
for (i = 0; i < n; ++i)
printf("%d%c%d%c%d\n", day[i], ca[i], month[i], cb[i], year[i]);
/*print all the date in ascending order*/
}
/*find out wheter it's leap year or not*/
int leapyear(int year)
{
if (year % 4000 == 0)
{
return 1;
}
else if (year % 400 == 0)
{
return 1;
}
else if (year % 40 == 0)
{
return 1;
}
else if (year % 4 == 0)
{
return 1;
}
else
{
return 0;
}
}
/*find out the days for the month input*/
int dater(x)
{
int y=0;
switch(x)
{
case 1: y=0; break;
case 2: y=31; break;
case 3: y=59; break;
case 4: y=90; break;
case 5: y=120; break;
case 6: y=151; break;
case 7: y=181; break;
case 8: y=212; break;
case 9: y=243; break;
case 10:y=273; break;
case 11:y=304; break;
case 12:y=334; break;
default: fprintf( stderr, "the value entered is not right\n");
}
return y;
}
void getdates(int f)
{
for(i=0;i<f;i++)
{
q = i+1;
printf("Please Enter %i dates (DD/MM/YYYY or DD-MM-YYYY) and
Press ENTER to finish each one\n", n);
scanf("%d%c%d%c%d", &dayn, &c1, &monthn, &c2, &yearn);
printf("You have entered %i date %i%c%i%c%i\n", q, dayn, c1, monthn, c2, yearn);
day[i] = dayn;
month[i] = monthn;
year[i] = yearn;
ca[i] = c1;
cb[i] = c2;
}
return;
}
int *caldays(int p)
{
for (i=0; i < p-1; i++)
{
leap = leapyear(year[i]);
t = 0;
if(leap == 1)
{
t++;
}
/*if there is a leap add one day */
/*find for the days for month entered*/
d = dater(month[i]);
/* find out the total days from the date entered begin with 0 days*/
d = d + dayn + t + (yearn * 365);
dd[i] = d;/*put all the days in an array*/
}
return dd;
}
The mistake is instead of
caldays(n);
for (i = 0; x < n; ++i)
in main function, it should be
caldays(n);
for (i = 0; i < n; ++i)
In getdates() and wherever required, use "%d %c %d %c %d" instead of %d%c%d%c%d.
As soon as you get the program working,I suggest you to post it for review on https://codereview.stackexchange.com/. The experienced people there will help you tear apart your code and make your life easier for better understanding of readability of code.
I recommend you to read Difference between format specifiers %i and %d in printf.
While posting question do follow MCVE from next time if possible.
I am having trouble refining some code. My code takes a number "n" and calculates that many prime numbers. I need to display 10 primes per line of output data. Any tips would be appreciated.
#include <stdio.h>
int main()
{
int n, i = 3, count, c;
printf("How many primes would you like?");
scanf("%d",&n);
if ( n >= 1 )
{
printf("2");
}
for ( count = 2 ; count <= n ; )
{
for ( c = 2 ; c <= i - 1 ; c++ )
{
if ( i%c == 0 )
break;
}
if ( c == i )
{
printf(" %d",i);
count++;
}
i++;
}
return 0;
}
Just try
printf(" %5d", i);
/* ^ to help align the numbers
and
if ((count + 1) % 10 == 0)
fputc(stdout, '\n');
fix for the first time when you already print 2.
bool is_prime(int anyNum) //takes an integer array returns, is_prime
{
bool is_prime = true;
for (int c = 2; c <= anyNum - 1; c++)
{
if (anyNum % c == 0)
{
//printf("%d is not prime\r\n" , anyNum);
is_prime = false;
}
}
return is_prime;
}
int main()
{
int num_primes;
printf("How many primes would you like: ");
std::cin >> num_primes;
printf("\r\nScanned Primes Are---\r\n");
int foundPrimes = 0;
int x = 0;
for (; x <= num_primes; x++)
{
bool gotLuckyFindingPrime = is_prime( x );
if (gotLuckyFindingPrime)
{
if (foundPrimes % 10 == 0)
{
printf("\r\n");
}
printf(" %d", x);
foundPrimes = (foundPrimes + 1) % 10;
}
}
}
Does handle ten digit showing on cmd too, you can experiment with formatting