I'm trying to make a one-month calendar in C. This code kind of works, but for some inputs the spacing is off. I don't quite know how to fix it.
Also, if you have a way of making a one month calendar that involves less code than this, that would be great, since I have to regurgitate this on a test in about an hour.
Thanks!
int main() {
int spaces, days_in_month, day_of_week, i;
printf("Please enter the numier of days in the month:\n");
scanf("%d", &days_in_month);
printf("Please enter starting day of the week:\n");
scanf("%d", &day_of_week);
spaces = day_of_week - 1;
printf("Here's your calendar:\n");
for (i=0; i<spaces; i++)
printf(" ");
for (i=1; i<=(7-spaces); i++)
printf(" %d", i);
printf("\n");
if ((i-spaces) >10 && (i-spaces) < 14-spaces)
printf(" ");
for (i=(8-spaces); i<=(10-spaces); i++)
printf(" %d", i);
for (i=(11-spaces); i<=(14-spaces); i++)
printf(" %d", i);
printf("\n");
for (i=(15-spaces); i<=(21-spaces); i++)
printf(" %d", i);
printf("\n");
for (i=(22-spaces); i<=(28-spaces); i++)
printf(" %d", i);
printf("\n");
for (i=(29-spaces); i<=days_in_month; i++)
printf(" %d", i);
return 0;
}
Use %2d instead of %d so if a day has number 1...9 printf inserts a space for you.
How was your test?
Here is one simpler way to approach it (ignoring input validation):
// Normalize day of week to be 0-6 rather than 1-7.
day_of_week -= 1;
// Pad the first line of the calendar.
for(i = 0; i < day_of_week; i++)
printf(" ");
// For each day in the month...
for(i = 1; i <= days_in_month; i++)
{
// Print the date for the current day_of_week.
// '%3d' will print the value padding with spaces if necessary such that
// at least 3 characters are written.
printf("%3d", i);
// Increment the day_of_week.
// The modulo operator '% 7' will cause day_of_week to wrap around to 0
// when day_of_week reaches 7.
day_of_week = (day_of_week + 1) % 7;
// if the new day_of_week is 0, output a newline to start at the
// beginning of the next line.
if(day_of_week == 0)
printf("\n");
}
A sample run produces the following output:
$ ./calendar.exe
Please enter the numier of days in the month:
28
Please enter starting day of the week:
6
Here's your calendar:
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28
#include<stdio.h>
int main(void)
{
int start_day, days_in_month, i, day_of_week;
printf("Enter start day: ");
scanf("%d", &start_day);
printf("Enter days in month: ");
scanf("%d", &days_in_month);
for(i = 1 ; i < start_day; i++) {
printf(" ");
}
for(i = 1; i <= days_in_month; i++) {
printf("%2d ", i);
if((i + start_day - 1)%7 ==0) {
printf("\n");
}
}
return 0;
}
Related
#include <stdio.h>
int main(void) {
int Choice = 0;
int monNum[12][2] = {
{1,31},
{2,28},
{3,31},
{4,30},
{5,31},
{6,30},
{7,31},
{8,31},
{9,30},
{10,31},
{11,30},
{12,31}
};
int i, j;
printf("Enter a number between 1 and 12 (for a month), 99 for all months. (0 to quit):");
scanf("%d", &Choice);
for(i=0; i < 12; i++) {
for(j=0; j < 2; j++) {
}
}
for(i=0; i < 12; i++) {
for(j=0; j < 2; j++) {
if ((Choice >= 1) && (Choice <= 12)){
printf("In month %d there are %d days\n", monNum[i][j]);
}
}
}
return 0;
}
Current output is as follows:
In month 1 there are 4200816 days
In month 31 there are 4200816 days
In month 2 there are 4200816 days
In month 28 there are 4200816 days
So on...
What it should be:
In month 1 there are 31 days.
So on...
There will be more if else later on to make the functions stated in the first print work, but for now I am lost in space on the 2D array part. If you can help it will be greatly appreciated.
How to correctly print elements out of two dimensional array (?)
Save time, enable all warnings
printf("In month %d there are %d days\n", monNum[i][j]);
warning: format '%d' expects a matching 'int' argument [-Wformat=]
printf("In month %d there are %d days\n", monNum[i][j]);
| ~^
printf() is only supplied 1 int when 2 are expected.
printf("In month %d there are %d days\n",
monNum[Choice-1][0], monNum[Choice-1][1]);
Other issues apply.
There area number of fundamental problems here:
The first nested for loop has no effect. This will not affect the output.
The if condition inside of the second nested for loop should be checking to see if the month number stored in Choice matches a month element in the table (i.e. Choice == monNum[i][0]).
The nested for loop (for(j=0; j < 2; j++)) is not needed and should be removed.
The printf statement should have three arguments (i.e. printf("In month %d there are %d days\n", monNum[i][0], monNum[i][1]);).
In short, the code modified to the following will produce the desired output:
#include <stdio.h>
int main(void) {
int Choice = 0;
int monNum[12][2] = {
{1,31},
{2,28},
{3,31},
{4,30},
{5,31},
{6,30},
{7,31},
{8,31},
{9,30},
{10,31},
{11,30},
{12,31}
};
int i;
printf("Enter a number between 1 and 12 (for a month), 99 for all months. (0 to quit):");
scanf("%d", &Choice);
for(i=0; i < 12; i++) {
if (Choice == monNum[i][0]){
printf("In month %d there are %d days\n", monNum[i][0], monNum[i][1]);
}
}
return 0;
}
I'm a beginner C programmer and a college freshman.
I need a little help here with a test i'm working on here.
I want to make a nested loop that shows a sorted number. Sorta like this:
1
3 2
4 5 6
10 9 8 7
11 12 13 14 15
21 20 19 18 17 16
22 23 24 25 26 27 28
... ... ... and so on, depending the limit of rows you input
I already tried to make a crude trial-and-error test code:
int i;
int j;
int limit;
int number1 = 1;
int number2 = 3;
int spesial = 0;
printf("Input limit : ");
scanf("%d", &limit);
for (i=1;i<=limit;i++)
{
for(j=1;j<=i;j++)
{
if (i%2==0)
{
printf("%d ", number2);
number2--;
}
else
{
printf("%d ", number1);
}
number1++;
}
if (i%2==0)
{
number2=(i*6)-i+(spesial*1);
spesial+=1;
}
printf("\n");
}
I managed to make it sorted to the 7th rows, but the rest are not..
help please...
I want to know if we could actually control the position of the output without sorta crude our way like this.
Also, sorry for my English... I'm not really from an English speaking country and this is my first time posting/question in this site.
Thank you for reading this lengthy question and I hope you have a good day and good night.
https://ideone.com/yCxpHo:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int rows;
int i, j;
int n = 0;
printf ("How many rows do you want? ");
if (scanf("%d", & rows) != 1 || rows < 1) return EXIT_FAILURE;
printf ("\n");
for (i = 1; i <= rows; ++ i) {
for (j = 0; j < i; ++ j) {
printf ("%4d", n + (i % 2 == 0 ? i - j : j + 1));
}
printf ("\n");
n = n + i;
}
return EXIT_SUCCESS;
}
It can be more convenient to create another function that will calculate the biggest number of a row (I called it lineMax).
int lineMax(int num){
int cnt=0;
for (int i=1;i<=num;i++)
cnt+=i;
return cnt;
}
void main(){
int i,j,limit;
printf("Input limit : ");
scanf("%d", &limit);
for(i=1;i<=limit;i++){
if(i%2==0){ //right to left
for(j=lineMax(i);j>=lineMax(i-1)+1;j--)
printf("%d ",j);
}
else{ //left to right
for(j=lineMax(i-1)+1;j<=lineMax(i);j++)
printf("%d ",j);
}
printf("\n");
}
}
You are making a lot of special cases with number1, number2 and special. This will not work for bigger numbers.
One way is to calculate count which will give you the value to start from in each loop of j. count += i and then every time print count -j
count = 0;
for (i=1;i<=limit;i++)
{
count += i;
for(j=0;j< i;j++)
{
printf ("%d ",count-j);
}
printf("\n");
}
Im having an issue with my code im trying to do for class. I have to make every prime number in a one month calendar printed out with "p", instead of the number for that day in the month. THe code i have here only prints out the first 3 days of the month with p, which is incorrect, and the rest of the days are the regular numbers. Can someone help please?:
int i, ndays, start, prime=1, p, day=0, array[32];
printf("Enter number of days in month: ");
scanf("%d", &ndays);
printf("Enter starting day of the week (1=Sun, 7=Sat): ");
scanf("%d", &start);
for(i=0; i<start-1; i++)
printf("%3c", ' ');
for(i=1;i<=ndays; i++){
day++;
array[i-1] = day;
for(p=2; p<day; p++){
if(day%p == 0)
prime = 0;
}
if(prime == 1)
printf("%3c", 'p');
else
printf("%3d", day);
if((i+start-1)%7 == 0)
printf("\n");
}
system("pause");
return 0;
}
I feel quite 'stupid' as asking this question but if anyone can show me the methods to modify the input result appeared on the command window.
Example:
I want to sort 5 numbers (1, 3, 4, 7, 5) in smallest-to-biggest order and the result on the command window must be:
input: 1 3 4 7 5 /* 1 line input */
output: 1 3 4 5 7 /* 1 line output */
Edit:
Here is my code
for (i = 0; i < 5; i++)
{
scanf("%d ", &array[i]);
}
If I use this code the result on command window must be:
1
3
4
7
5
But I want all the input number in only 1 line as:
1 3 4 7 5
So what do I have to do with my code?
Regarding to your edited question, just replace "%d " with "%d".
#include <stdio.h>
#define N 5
int main(void){
int i, j, array[N];
printf("Please enter the %d numbers.\n", N);
printf("input : ");
for(i=0;i<N;++i){
scanf("%d", &array[i]);
if(i!=0){
for(j=i;j>0 && array[j-1] > array[j];--j){
//swap array[j] and array[j-1]
int tmp = array[j];
array[j] = array[j-1];
array[j-1] = tmp;
}
}
}
printf("output : ");
for(i=0;i<N;++i){
if(i!=0)
putchar(' ');
printf("%d", array[i]);
}
putchar('\n');
return 0;
}
I'm trying to make a one-month calendar that prints the sum of the last row of days.
The output of this is correct for the calendar, but the sum keeps printing out that it's 0. For an input of 3=day_of_week and 30=days_in_month, the sum should be 26+27+28+29+30 = 140
Thanks.
int main() {
int day_of_week, days_in_month, i, row=1, array[31], sum=0, a;
printf("Enter the day of the week 1=sun, 2=mon, 3=tue, 4=wed, 5=thurs, 6=fri, 7=sat\n");
scanf("%d", &day_of_week);
printf("Enter the number of days in this month:\n");
scanf("%d", &days_in_month);
for (i=0; i<3*day_of_week; i++)
printf(" ");
for (i=1; i<=days_in_month; i++) {
printf("%3d", i);
array[i] = i;
day_of_week++;
if (day_of_week%7==0){
printf("\n");
}
}
printf("\n");
for (a=days_in_month; a>=(days_in_month-(7-day_of_week)); a--)
sum+=array[a];
printf("sum of last row is %d\n", sum);
return 0;
}
I don't understand why you are doing ++day_of_week,
something like this should work better:
int main()
{
int day_of_week, days_in_month, i, row=1, array[31], sum=0, a;
printf("Enter the day of the week 1=sun, 2=mon, 3=tue, 4=wed, 5=thurs, 6=fri, 7=sat\n");
scanf("%d", &day_of_week);
printf("Enter the number of days in this month:\n");
scanf("%d", &days_in_month);
for (i = 0; i < 3 * day_of_week; i++)
printf(" ");
for (i = 1; i <= days_in_month; i++)
{
printf("%3d", i);
array[i] = i;
if (i % 7 == 0)
printf("\n");
}
printf("\n");
for (a=days_in_month; a>=(days_in_month-(7-day_of_week)); a--)
sum+=array[a];
printf("sum of last row is %d\n", sum);
return 0;
}
You have
for (a=days_in_month; a>=(days_in_month-(7-day_of_week)); a--)
but day_of_week does not remain constant in your program and changes before with this statement:
day_of_week++;
Use a second variable to increment and do not modify day_of_week after scanf.
One problem is here:
for (i=1; i<=days_in_month; i++) {
printf("%3d", i);
array[i] = i;
day_of_week++;
if (day_of_week%7==0){
printf("\n");
}
}
You are allowing day_of_week to go out of range. Your code expects that value to be no more than 7. This loop will result in that variable being set to the value the user entered plus (days_in_month - 1). In your final for loop, the statement 7 - day_of_week will likely be negative, which will throw the rest of your code off.
You are sort of checking for overflow when you test the variable modulo 7 and print a newline. When you do that, set day_of_week = 0 as well.
Also, calculate (days_in_month-(7-day_of_week)) and store it in a temporary variable as soon as you get the input from the user. Since you're manipulating these variables inside your code, your final for loop probably isn't using the values that you think it's using. Alternatively, don't modify the variables that you use for user input and create other variables to use as temporaries.