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

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

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

how to record multiple max values from a 2D array in C using stdio.h

I have this homework from my class where I am asked to hard-code 30 values into a 2D array (4 weeks, 7 days where temperature was recorded daily). Then make my program so that it finds and prints the max value, and the day&week it was recorded. However there are 3 max values on 3 separate days. I assume that all 3 cases must be printed.
I am still a beginner and I haven't encountered a problem like this before. I wrote a code to print one max value of the 3. It is mentioned below:
#include <stdio.h>
#define y 4
#define x 7
int main()
{
int max = 0, week, i, j, day;
int table[y][x] = {
{32,31,30,31,32,33,32},
{33,32,34,35,34,36,36},
{34,34,36,36,37,38,38},
{38,37,36,35,34,33,32}};
for (i = 0; i < y; i++)
{
for (j = 0; j < x; j++)
{
if (max <= table[i][j])
{
max = table[i][j];
day = j + 1;
week = i + 1;
}
}
}
switch (day)
{
{ case 1: printf("The highest temperature %d was recorded on Monday of week %d\n", max, week); break; }
{ case 2: printf("The highest temperature %d was recorded on Tuesday of week %d\n", max, week); break; }
{ case 3: printf("The highest temperature %d was recorded on Wednesday of week %d\n", max, week); break; }
{ case 4: printf("The highest temperature %d was recorded on Thursday of week %d\n", max, week); break; }
{ case 5: printf("The highest temperature %d was recorded on Friday of week %d\n", max, week); break; }
{ case 6: printf("The highest temperature %d was recorded on Saturday of week %d\n", max, week); break; }
{ case 7: printf("The highest temperature %d was recorded on Sunday of week %d\n", max, week); break; }
}
return 0;
}
Can you guys help me to write this code in a way that if there are x number of max values, all cases of x values be printed with their days and week. Also since we haven't covered all libraries in my class. They expect me to only use stdio.h to write programs.
Thank you for taking time to read. also thank you in advance for your replies.
PS: I would really appreciate if you guys can suggest solutions with using, <stdio.h>, arrays, pointers and loops, because I haven't still covered other aspects of coding yet.
Your code is functional and it does produce the last day with highest temperature recorded. Yet here are some areas of improvement:
Avoid defining simple lowercase identifiers such as x and y with #define.
You should use an array for the names of the days of the week to avoid a lengthy switch statement with lots of redundant code.
You should initialize max to the value of the first temperature recorded: as coded, you would fail to find the largest if all temperatures were negative.
You noticed the maximum temperature was recorded on multiple days, this is a very good point and the specification is somewhat ambiguous regarding what to output. Producing multiple results may not be what is expected, but certainly a bonus if the assignment is verified by hand.
To print all matching days, first determine the maximum temperature, then use a second loop to enumerate the days it was recorded.
Here is a modified version:
#include <stdio.h>
#define NWEEKS 4
#define WEEKDAYS 7
int main(void) {
int max, week, day;
int table[NWEEKS][WEEKDAYS] = {
{ 32, 31, 30, 31, 32, 33, 32 },
{ 33, 32, 34, 35, 34, 36, 36 },
{ 34, 34, 36, 36, 37, 38, 38 },
{ 38, 37, 36, 35, 34, 33, 32 }};
const char *dayname[WEEKDAYS] = {
"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday" };
max = table[0][0];
for (week = 0; week < NWEEKS; week++) {
for (day = 0; day < WEEKDAYS; day++) {
if (max < table[week][day]) {
max = table[week][day];
}
}
}
printf("The highest temperature is %d\n", max);
for (week = 0; week < NWEEKS; week++) {
for (day = 0; day < WEEKDAYS; day++) {
if (table[week][day] == max) {
printf("It was recorded on %s of week %d\n",
dayname[day], week + 1);
}
}
}
return 0;
}
Try this out. Store the days' details in a linked list if there are multiple.
#include<stdio.h>
#define y 4
#define x 7
typedef struct record{
int week;
int day;
record* next;
}dayRecord;
int main()
{
int max=0,week,i,j,day;
int table[y][x]={{32,31,30,31,32,33,32},{33,32,34,35,34,36,36},{34,34,36,36,37,38,38},{38,37,36,35,34,33,32}};
dayRecord* list = NULL;
for(i=0; i<y ; i++ )
{
for(j=0; j<x ; j++)
{
if(max<table[i][j])
{
max=table[i][j];
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = NULL;
list = d;
}else if(max == table[i][j]){
dayRecord* d = malloc(sizeof(dayRecord));
d->day=j+1;
d->week=i+1;
d->next = list;
list = d;
}
}
}
while(list->next != NULL){
int day = list->day;
int week = list->week;
list = list->next;
switch(day)
{
{case 1: printf("The highesest tempreture %d was recorded on Monday of week %d\n",max,week);break;}
{case 2: printf("The highesest tempreture %d was recorded on Tuesday of week %d\n",max,week);break;}
{case 3: printf("The highesest tempreture %d was recorded on Wednesday of week %d\n",max,week);break;}
{case 4: printf("The highesest tempreture %d was recorded on Thursday of week %d\n",max,week);break;}
{case 5: printf("The highesest tempreture %d was recorded on Friday of week %d\n",max,week);break;}
{case 6: printf("The highesest tempreture %d was recorded on Saturday of week %d\n",max,week);break;}
{case 7: printf("The highesest tempreture %d was recorded on Sunday of week %d\n",max,week);break;}
}
}
return 0;
}
Well if you are a beginner, I would say the best thing to do is to have 2 sets of iterators.
An iterator is basically
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
// Do something here.
}
}
So what you do is, find the maximum temp in the first iterator, and have an if condition matching the max value to the current value in the second iterator. If the value matches, run the switch case that you have already written.
This way you don't have to program a complicated link list.
Though, use of link list is highly advised if you want to have a carrier in programming.

Trying to Print every prime number in a Calendar with p

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

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