Trying to Print every prime number in a Calendar with p - c

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;
}

Related

How to correctly print elements out of two dimensional array?

#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 trying to get the counter to work, how many months have a higher income than the same month from last year

The line of code I'm having trouble with has to do with counting a number of months, I'm passing a value up from a function that I thought would work but I'm not getting a proper readout from it. I'm sure I'm missing something stupid or the mistake I made is very basic but I can't seem to get it right. I would greatly appreciate any help I can get. please excuse the many printf statements, I was trying to find where the error was.
#include <stdio.h>
#include <stdlib.h>
void DisplayInstructions();//function 1
void HigherSales(float yearOne, float yearTwo, int k);//fuction 2
int months (float yearOne, float yearTwo);//function 3
void main()
{
float yearOne[12];//variables
float yearTwo[12];
int i = 0;
int j = 0;
int k = 0;
int count = 0;
DisplayInstructions();//calling first function
printf(" \n");
for (i = 0; i<12; i++)//scanf for entering sales values
{
j= i+1;
printf("enter the sales figures for month %d in year one \n",j);
scanf_s("%f",&yearOne[i]);
}
for (i = 0; i<12; i++)
{
j= i+1;
printf("enter the sales figures for month %d in year two \n",j);
scanf_s("%f",&yearTwo[i]);
} //end of entering sales
k=0;
for (i = 0; i<12; i++)//populating function 2
{
k++;
HigherSales(yearOne[i], yearTwo[i], k);
}
printf("\n count before going into the for loop is %d \n",count);
for (i = 0; i<12; i++)
{
months(yearOne[i], yearTwo[i]);//populating function 3
printf("before going into count calculation months is reading %f",months(yearOne[12], yearTwo[12]));
count = count + months(yearOne[12], yearTwo[12]); //calling function 3 so that i get the sum of times y1 < y2
printf(" \n after calc count is %d \n after calc monts is returning %f", count, months(yearOne[12], yearTwo[12]));
}
printf(" \n the number of months in year two where sales have increased is %d \n", count);
system("pause");
}
void DisplayInstructions() //function 1 printf's
{
printf("\n");
printf("this program consists of one function called 'DisplayInstructions' \n");
printf("the function takes no arguments and simply dispays this message \n");
printf("it is innitalised above main, called inside main and described below main \n");
}
void HigherSales(float yOne, float yTwo, int g)
{
if(yOne < yTwo) //function 2 comparing logic
{
printf("month %d in year two had higher sales than the same month the year prior \n", g);
}
}
int months(float monthOne, float monthTwo)
{
int m = 0;
if(monthOne < monthTwo )
{
m = m + 1;
printf("\n in the mothhs function, m loop, m is %d \n", m);
return m;
}
}
The function int months(float monthOne, float monthTwo) does not work as you probably expect.
With int m = 0; the variable m always is initialized with 0 every time this function is called, therefore m = m + 1 won't count up how often the condition is true - its either 0 or 1
To make the function remember the value of m from its last call you need to declare it static int m = 0;
But in your case this wouldn't be the best idea either because you would only want to know the last return value (when all months are iterated).
Furthermore function months only has a return value if the condition is true - your compiler should have complained about that.
So if you want to make this simple comparison within a function which is called within a loop:
int months(float monthOne, float monthTwo)
{
if (monthOne < monthTwo)
return 1;
else
return 0;
}
for (i = 0; i<12; i++)
{
count += months(yearOne[i], yearTwo[i]);
}
In your context I cant figure out the purpose of count = count + months(yearOne[12], yearTwo[12]); - beside the fact that you are accessing the 13th element of your array which is invalid, it makes no sense to increment your counter by the same comparison every loop.
The modified code below addresses the errors I commented under your post as well as some others (see comments in code.) I automated inputs using rand() to speed up testing, but left all your original code. Comment out the auto populate sections for your purposes.
Note: Not all logic errors are addressed, but enough to get you started. Read the comments left in the code.:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void DisplayInstructions();//function 1
void HigherSales(float yearOne, float yearTwo, int k);//fuction 2
int months (float yearOne, float yearTwo);//function 3
const char month[][4] = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
void main()
{
float yearOne[12];//variables
float yearTwo[12];
int i = 0;
int j = 0;
int k = 0;
int count = 0;
srand(clock());//seed pseudo random number generator
DisplayInstructions();//calling first function
printf(" \n");
for (i = 0; i<12; i++)//scanf for entering sales values
{
j= i+1;
printf("enter the sales figures for month %d in year one \n",j);
yearOne[i] = rand();//Added to automate populating monthly sales numbers...
while(yearOne[i] < 500)
{
yearOne[i] = rand()%10000;//Added to automate populating monthly sales numbers...
Sleep(100);//pause execution for 0.1 seconds
}
printf("YearOne - %s: %lf\n", month[i], yearOne[i]);
//scanf("%f",&yearOne[i]);
}
for (i = 0; i<12; i++)
{
j= i+1;
printf("enter the sales figures for month %d in year two \n",j);
yearTwo[i] = rand();//Added to automate populating monthly sales numbers...
while(yearTwo[i] < 500)
{
yearOne[i] = rand()%10000;//Added to automate populating monthly sales numbers...
Sleep(100);//pause execution for 0.1 seconds
}
printf("YearTwo - %s: %lf\n", month[i], yearOne[i]);
//scanf("%f",&yearTwo[i]);
} //end of entering sales
k=0;
for (i = 0; i<12; i++)//populating function 2
{
k++;
HigherSales(yearOne[i], yearTwo[i], k);
}
printf("\n count before going into the for loop is %d \n",count);
for (i = 0; i<12; i++)
{ //Hint: In this for loop, you are calling months function more
//than you want to. Consider assigning a variable to read it's
//output, then use that variable in the printf statements.
months(yearOne[i], yearTwo[i]);//populating function 3
// printf("before going into count calculation months is reading %f",months(yearOne[i], yearTwo[i]));
printf("before going into count calculation months is reading %d",months(yearOne[i], yearTwo[i]));//changed format type to match variable type
count = count + months(yearOne[i], yearTwo[i]); //calling function 3 so that i get the sum of times y1 < y2
//printf(" \n after calc count is %d \n after calc monts is returning %f", count, months(yearOne[i], yearTwo[i]));
printf(" \n after calc count is %d \n after calc monts is returning %d", count, months(yearOne[i], yearTwo[i]));//changed format type to match variable type
}
printf(" \n the number of months in year two where sales have increased is %d \n", count);
// system("pause");
printf("hit any key to continue.");
getchar(); //this method is more idomatic and portable to pause code
}
void DisplayInstructions() //function 1 printf's
{
printf("\n");
printf("this program consists of one function called 'DisplayInstructions' \n");
printf("the function takes no arguments and simply dispays this message \n");
printf("it is innitalised above main, called inside main and described below main \n");
}
void HigherSales(float yOne, float yTwo, int g)
{
if(yOne < yTwo) //function 2 comparing logic
{
printf("month %d in year two had higher sales than the same month the year prior \n", g);
}
}
int months(float monthOne, float monthTwo)
{
static int m = 0;//use static here to allow m to keep value from call to call.
//static extends life of variable until program ends.
if(monthOne < monthTwo )
{
m = m + 1;
printf("\n in the mothhs function, m loop, m is %d \n", m);
}
return m;//moved from inside loop, otherwise return will not occur
//for some evaluations.
//Perhaps you would prefer an else statement
}

Pick and print a certain group of integers out of an array

Let's say that we have an array with the temperatures of a whole month typed by the user.
int april[31];
int i;
for(i=0; i<31; i++)
{
printf("Give today's temperature: \n");
scanf("%d", &april[i]);
}
for(i=0; i < 5; i++)
printf("Day %d = %dC\n", i+1, april[i]);
Next, the user types two numbers (days of the month, <=31 && >0), which the program will show on the screen the temperatures between those numbers. Example:
GIVE A SPACE OF 2 DAYS SO I'LL SHOW YOU THE TEMPS(1-31):
>>2 10
THE TEMPS ARE: 28, 25, 23, 27, 26, 25, 24, 29, 30
Should I just make a new array for them, or is there any way to kind of "snatch" a group of consecutive array values and print them?
int start, end;
printf("GIVE A SPACE OF 2 DAYS SO I'LL SHOW YOU THE TEMPS(1-31):");
scanf("%d %d", &start, &end);
for (int k = start - 1; k < end - 1; k++) {
printf("%d ", april[k]);
}

Calendar in c with sum of last row

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.

one month calendar in c

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;
}

Resources