Making a payroll assignment in my intro to C class and the objective is to read a text file and then output their pay information to a text file. I've received some incompatible pointer errors and CodeBlocks is only giving me an output file which contains a bunch of gibberish. I posted the text file below with my comments so you can understand what I'm doing with my code. Any help will do, appreciate it guys and gals.
2 (this is the number of employees)
5.50 (pay rate for employee 0)
10.00 (pay rate of employee 1)
3 (number of weeks)
2 (data in relation to this week)
1 10 30 13 30 (reads: employee 1 clocked in at 1030, clocked out 1330)
0 7 0 16 30 (reads: employee 0 clocked in at 700, clocked out 1630)
4
0 9 0 14 30
1 7 0 23 0
1 9 0 22 0
1 7 20 23 20
3
0 10 0 15 0
1 8 0 12 0
0 9 30 11 30
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define EMPLOYEES 20
#define WEEKS 10
/*prototype functions: One to take in the total hours, the other printing out the text file*/
double totalHrs(int hi, int mi, int ho, int mo);
void printLog( int weeks, int numEmp, double Emp[numEmp][2], int wkHrs[numEmp][weeks]);
int main()
{
/*variables for number of employees, number of weeks, shifts and hours in and hour along with minutes.*/
int n, weeks, shifts;
int empId, hrIn, minIn, hrOut, minOut, tmpWkHrs;
double m;
FILE *ifp;
/*Read clock text file*/
ifp = fopen("clock.txt", "r");
if(ifp == NULL)
{
exit(1);
}
/*scan in the number of employees*/
fscanf(ifp, "%d", &n);
if(n>EMPLOYEES)
{
printf("Employees provided is greater than 20");
exit(0);
}
double emp[n][2];
for(int i=0; i<n;i++)
{
fscanf(ifp, "%lf", &m);
emp[i][1] = m;
}
fscanf(ifp, "%d", &weeks);
double empWkHrs[n][weeks];
for(int i = 0; i < weeks; i++)
{
fscanf(ifp, "%d", &shifts);
for (int j = 0; j < shifts; j++)
{
fscanf(ifp, "%d", &empId);
fscanf(ifp, "%d", &hrIn);
fscanf(ifp, "%d", &minIn);
fscanf(ifp, "%d", &hrOut);
fscanf(ifp, "%d", &minOut);
emp[empId][0] += totalHrs(hrIn, minIn, hrOut, minOut);
empWkHrs[empId][i] += totalHrs(hrIn, minIn, hrOut, minOut);
}
}
printLog(weeks, n, emp, empWkHrs);
fclose(ifp);
return 0;
}
/*------------------------------------------------------------------------------------*/
double totalHrs(int hi, int mi, int ho, int mo)
{
double totalHrs = (double)abs(ho - hi);
double totalmins = (double)abs(mo - mi);
totalHrs += totalmins/60;
return totalHrs;
}
/*------------------------------------------------------------------------------------*/
/*Print function that prints out the information onto a text file named payroll*/
void printLog( int weeks, int numEmp, double Emp[numEmp][2], int wkHrs[numEmp][weeks])
{
FILE *ofp;
ofp = fopen("payroll.txt", "w+");
if(ofp == NULL)
{
exit(1);
}
fprintf(ofp, "Number of employees: %d\n", numEmp);
fprintf(ofp, "Number of weeks: %d\n", weeks);
/*Loop through to print out all of the information in regards to every week*/
for(int i = 0; i < weeks; i++)
{
fprintf(ofp, "Wk%d\n", weeks+1);
fprintf(ofp,"EmpID\t Hours\t Pay\n");
for(int j = 0; j < numEmp; j++)
{
/*If statement for an employee that works over 40 hours */
if(wkHrs[j][i] > 40)
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, wkHrs[j][i], (Emp[j][1]*40)+(wkHrs[j][i]-40)*1.5*Emp[j][1]);
}
else
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, wkHrs[j][i], Emp[j][1]*wkHrs[j][i]);
}
}
}
fprintf(ofp, "Total\n");
fprintf(ofp, "EmpID\t Hours\t Pay\n");
for(int j = 0; j < numEmp; j++)
{
fprintf(ofp, "\t %d\t %lf\t %lf", j, Emp[j][0], Emp[j][1]*Emp[j][0]);
}
fclose(ofp);
}
This is what the output should look like.
Number of employees: 2
Number of weeks: 3
Wk 1
EmpID Hours Pay
0 9.50 52.25
1 3.00 30.00
Wk 2
EmpID Hours Pay
0 5.50 30.25
1 45.00 450.00
Wk 3
EmpID Hours Pay
0 7.00 38.5.00
1 4.00 40.00
Total
EmpID Hours Pay
0 22.00 121.00
1 52.00 520.00
This is what I get in my payroll.o file
Number of weeks: %d
Wk%d
EmpID Hours Pay
%d %lf %lfTotal
qÄzRxê$#¯ˇˇˇˇˇˇAÜC
$D˚ˇˇˇˇˇˇqAÜC
$lp˚ˇˇˇˇˇˇAÜC
-c-
In your printLog the last parameter should be of type double, not int.
Related
EDIT: New Problem, now I get a totally different output than the one I need. The following is how I have it written, assignment instruction is on the bottom, please and thank you all!
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE * ifp = NULL;
char filename[20];
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
while (ifp == NULL){
/*PROMPT USER FOR INPUT FILENAME*/
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
/*OPEN INPUT FILE*/
ifp = fopen(filename, "r");
}
int totalSize = 0;
fscanf(ifp, "%d", &totalSize);
int id[totalSize];
char category[totalSize];
int handCombatPt[totalSize];
int distCombatPt[totalSize];
int observationPt[totalSize];
int concealPt[totalSize];
int agilityPt[totalSize];
float ranking[totalSize];
int row=0;
for (row=0; row<totalSize; row++) {
fscanf(ifp, "%d %c %d %d %d %d %d\n", id+row, category+row, handCombatPt+row, distCombatPt+row, observationPt+row, concealPt+row, agilityPt+row);
}
for (row=0; row<totalSize; row++) {
if (category[row] == 'A') {
ranking[row] = (handCombatPt[row] + distCombatPt[row]*2 + observationPt[row]*2 + concealPt[row] + agilityPt[row]*5)/10.0;
}
if (category[row] == 'C') {
ranking[row] = (handCombatPt[row]*5 + distCombatPt[row]*5 + observationPt[row] + concealPt[row] + agilityPt[row]*2)/10.0;
}
if (category[row] == 'S') {
ranking[row] = (handCombatPt[row] + distCombatPt[row] + observationPt[row]*5 + concealPt[row]*5 + agilityPt[row]*2)/10.0;
}
}
int firstA, firstS, secondS, firstC, secondC;
for (row=0; row<totalSize; row++) {
if (category[row]=='A' && ranking[firstA] < ranking[row]) {
firstA = row;
}
if (category[row]=='S' && ranking[firstS] < ranking[row]) {
secondS = firstS;
firstS = row;
}
else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
secondS = row;
}
if (category[row]=='C' && ranking[firstC] < ranking[row]) {
secondC = firstC;
firstC = row;
}
else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
secondC = row;
}
}
printf("A : %d %f \n", id[firstA], ranking[firstA]);
printf("C : %d %f \n", id[firstC], ranking[firstC]);
printf("C : %d %f \n", id[secondC], ranking[secondC]);
printf("S : %d %f \n", id[firstS], ranking[firstS]);
printf("S : %d %f \n", id[secondS], ranking[secondS]);
return 0;
}
And here's the input.txt file:
10
14 A 447 252 68 34 978
2 C 230 299 597 180 9
27 A 318 220 97 28 1317
32 C 563 450 547 112 28
8 C 669 260 200 36 171
11 S 179 45 1342 732 174
19 S 74 249 861 1165 6
21 A 757 240 97 119 2032
15 S 275 177 588 577 52
6 C 886 401 327 109 48
The program needs to output the follow:
A: 21 1171.00
C: 6 696.70
C: 32 578.00
S: 11 1094.20
S: 19 1046.50
Any help would be greatly appreciated!
EDIT: Here is the assignment in case it helps anyone understand what I'm trying to do
Problem: Mentorship
It is time for your friend to select their ninja mentors! Ninja students are able to select several mentorsfrom the class of higher level students to learn special skills from. Skills are categorized as Stealth (S),Combat (C), and Agility (A). Your friend will be provided with a file of older students that has their nameand rankings for the different skills. They can then choose 5 mentors to learn from. To assist, your program should read in all of the student’s information and print out the two bestcombat mentors, the two best stealth mentors, and the best agility mentor. If your friend has been adiligent student, they will be able to select these best options! If not, they will need to go down the listand select other mentors.Combat Skills are split into Hand to Hand and Distance. Stealth skills are split into Observation andConcealment. Agility is a singular category.
Input File Format
The first line of the input file will contain a single integer n (5 ≤ n ≤ 100), denoting the number ofpotential mentors, for which information is listed in the file. The following n lines will have all theinformation for all the mentors with one mentor's information on a single line. Each line will have thefollowing format:ID Category HandCombatPts DistanceCombatPts ObservationPts ConcealPts AgilityPtsID will be a positive integer representing the potential mentor.
Category will be a single character, either 'C', 'S' or 'A', for combat, stealth or agility, respectively.HandCombatPts will be an integer representing the number of points that student was given last year bytheir hand to hand combat instructor. DistanceCombatPts will be an integer representing the number of points that student was given lastyear by their distance combat instructor.ObservationPts will be an integer representing the number of points that student was given last year by
their observation and spying skills instructor.
ConcealPts will be an integer representing the number of points that student was given last year by their
concealment and disguise instructor.
AgilityPts will be an integer representing the number of points that student was given last year by theiragility and acrobatics instructor.
How to Compute a Ranking
For each potential mentor, their ranking will be a summation weighted by their category. If they are a potential combat mentor their ranking should be:(HandCombatPts*5 + DistanceCombatPts*5 + ObservationPts + ConcealPts + AgilityPts*2)/10If they are a potential stealth mentor their ranking should be:(HandCombatPts + DistanceCombatPts + ObservationPts*5 + ConcealPts*5 + AgilityPts*2)/10If they are a potential agility mentor their ranking should be:(HandCombatPts + DistanceCombatPts*2 + ObservationPts*2 + ConcealPts + AgilityPts*5)/10
Program Specification
You must use arrays to solve the problem.
Your program should first prompt the user for the name of the input file. Then, your programshould process the input file and write the five best mentors for your friend. Each line shouldlist the category, the ID, and the ranking of the mentor, respectively, separated by spaces.Round the ranking to two decimal places. The mentors must be listed according to category asfollows: agility, followed by the two combat, followed by the two stealth. Both the combat andthe stealth mentors must be listed in descending order of ranking.
First:
'Program compiles but then “program.exe has stopped working'
I'm sorry to have to inform you that this is the usual behaviour. Writing code and getting it to compile is trivial compared with the effort and skill required to get it to do what you want. Testing and debugging is 99% of software development skill. That is why debuggers exist.
Next:
ALWAYS check the result of ALL system calls. In your case, specifically fopen().
...............
[sigh] 'after I set the uninitialized variables to 0, but now I get a completely different output then the one I need'
See above, especially the hint: 'That is why debuggers exist'. It is really MUCH easier for you to fix your problems than to use SO contributors remote-debug by text exchange. You have the actual code, (ie. not what you originally posetd), the environment, test files etc. We have what you are drip-feeding us, both in terms of what you are doing and what you are getting:(
You must learn to debug now, before you write even one more line of code. If you cannot debug, you cannot develop programs and should not try:(
#include<stdio.h>
#include<stdlib.h>
int main() {
FILE * ifp = NULL;
char filename[20];
while (ifp == NULL){
printf("What is the name of the input file?\n");
scanf(" %s", &filename);
ifp = fopen(filename, "r");
}
int totalSize = 0;
fscanf(ifp, "%d", &totalSize);
int id[totalSize];
char category[totalSize];
int handCombatPt[totalSize];
int distCombatPt[totalSize];
int observationPt[totalSize];
int concealPt[totalSize];
int agilityPt[totalSize];
float ranking[totalSize];
int row=0;
for (row=0; row<totalSize; row++) {
fscanf(ifp, "%d %c %d %d %d %d %d\n", id+row, category+row, handCombatPt+row, distCombatPt+row, observationPt+row, concealPt+row, agilityPt+row);
}
for (row=0; row<totalSize; row++) {
if (category[row] == 'A') {
ranking[row] = (handCombatPt[row] + distCombatPt[row]*2 + observationPt[row]*2 + concealPt[row] + agilityPt[row]*5)/10.0;
}
if (category[row] == 'C') {
ranking[row] = (handCombatPt[row]*5 + distCombatPt[row]*5 + observationPt[row] + concealPt[row] + agilityPt[row]*2)/10.0;
}
if (category[row] == 'S') {
ranking[row] = (handCombatPt[row] + distCombatPt[row] + observationPt[row]*5 + concealPt[row]*5 + agilityPt[row]*2)/10.0;
}
}
int firstA=0, firstS=0, secondS=0, firstC=0, secondC=0;
for (row=0; row<totalSize; row++) {
if (category[row]=='A' && ranking[firstA] < ranking[row]) {
firstA = row;
}
if (category[row]=='S' && ranking[firstS] < ranking[row]) {
secondS = firstS;
firstS = row;
}
else if (category[row]=='S' && ranking[secondS] < ranking[row]) {
secondS = row;
}
if (category[row]=='C' && ranking[firstC] < ranking[row]) {
secondC = firstC;
firstC = row;
}
else if (category[row]=='C' && ranking[secondC] < ranking[row]) {
secondC = row;
}
}
printf("A : %d %f \n", id[firstA], ranking[firstA]);
printf("C : %d %f \n", id[firstC], ranking[firstC]);
printf("C : %d %f \n", id[secondC], ranking[secondC]);
printf("S : %d %f \n", id[firstS], ranking[firstS]);
printf("S : %d %f \n", id[secondS], ranking[secondS]);
return 0;
}
This worked, I initialized everything to 0 and made a while loop for the file name, now it outputs corerctly, though more numbers after the decimal than I need but I think i can fix it with a .lf in the variable print part. If anyone can check that and let me know if they see any thing wrong with it please. Thank you all for the help!
I have this assignment that I can't figure out.
We have a file in the following format:
5
4
100 500 250 300
1
700
3
300 150 175
2
920 680
8
20 10 15 25 50 30 19 23
On the first line we have the total number of auctions.
Afterwards, each two rows represent an auction.
On the first row there is the number of bids. On the following row there are the actual bids.
For example the number 4 describes an auction with 4 bids (100,500,250,300).
My task is to determine the highest bid for each auction. This is what I've got so far. Any help will be appreciated.
#include <stdio.h>
int main() {
FILE * ifp;
char filename[100];
printf("File name\n");
scanf("%s", &filename);
ifp = fopen (filename, "r");
if (ifp == NULL) {
printf("Error, File could not be opened.\n");
return 1;
}
int i, num_auctions, auction, j, bid, max;
fscanf(ifp, "%d", &num_auctions);
for(i=0; i<num_auctions; i++) {
fscanf(ifp, "%d", &auction);
if (bid > max)
max = bid;
for(j=0; j<auction; j++){
fscanf(ifp, "%d", &bid);
printf("%d\n", bid);
}
printf("%d\n", max);
}
fclose(ifp);
return 0;
}
These are the problems in your code.
bid and max are used unintialised. Fix is to set them to 0 when they are declared.
The if (bid > max) check is in the wrong spot. It's only checking the last bid of each auction. The fix is to move that check into the inner for loop after the fscanf.
max needs to be cleared after each auction. The fix is to set max to 0 at the top of the outer for loop.
So I have a function built already that calculated 25 random temperatures and outputted them and had a max, min, and average feature. I'm now trying to incorporate input files and output files via txt.
I tried to do some research and plug in what I could (even if I barely understood it), can someone lend some light on my code?
int get_value(void);
void calc_results(void);
void read_temps(void);
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0; //For array loop
int j = 0; //For printf loop
float avg = 0;
void read_temps() {
char fname[128];
printf("Enter .txt file name \n");
scanf("%123s", fname);
strcat(fname, ".txt");
FILE *inputf;
inputf=fopen(fname, "w");
for (i = 0; i < 25; i++){
temp[i] = fname;
sum += temp[i];
}
}
int main () {
calc_results();
return 0;
};
void calc_results(void) {
FILE * fp;
fp = fopen("Output_temps.txt", "w+");
avg = ((sum)/(25));
max = temp[0];
for(i=1;i<25;i++){
if(max<temp[i])
max=temp[i];
};
min =temp[0];
for(i=1;i<25;i++){
if(min>temp[i])
min=temp[i];
};
fprintf("Temperature Conditions on October 9, 2015 : \n");
fprintf("Time of day Temperature in degrees F \n");
for(j=0;j<25;j++){
fprintf(" %d %d\n",j,temp[j]);
}
fprintf("Maximum Temperature for the day: %d Degrees F\n", max);
fprintf("Minimum Temperature for the day: %d Degrees F\n", min);
fprintf("Average Temperature for the day: %.1f Degrees F\n", avg);
fclose(fp);
};
There were a few errors in your code, the most critical one being it doesn't compile. If you're having issues you'll want to follow the instructions for how to make a ssce. You will get a much better response this way. Then explain clearly the specific issue you are having and what it is that is happening or not happening as opposed to what you expect.
With your code you seem to be assigning to your temp array the fname variable instead of reading in the int data from the user file.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// assuming you want a maximum of temperatures of 25
#define TEMP_NUMBER 25
// use a struct to maintain and the temparatur data in one place
// without resorting to using global variables or having functions
// that require numerous parameters.
struct temp_data {
char fname[128];
int max;
int min;
int sum;
int temps[TEMP_NUMBER];
float avg;
};
struct temp_data *temps_init(void)
{
// creates a pointer to struct temp_data to hold
// your various temparture related fields
struct temp_data *td = malloc(sizeof *td);
td->sum = 0;
td->avg = 0.0;
return td;
}
void read_temps(struct temp_data *td)
{
// in your sample code you have this set to "w", needs to be "r"
FILE *inputf = fopen(td->fname, "r");
// handle error
if (!inputf) {
perror("fopen");
exit(0);
}
for (int i = 0; i < TEMP_NUMBER; i++) {
// you were setting fname to the temparature array
// instead you need to scan the temp file
fscanf(inputf, "%d", &(td->temps[i]));
td->sum += td->temps[i];
}
}
void print_results(struct temp_data *td)
{
// a print function to separate logic
FILE *fp = fopen("Output_temps.txt", "w+");
if (!fp) {
perror("fopen");
exit(0);
}
fprintf(fp, "Temperature Conditions on October 9, 2015 : \n");
fprintf(fp, "Time of day Temperature in degrees F \n");
for(int i=0; i < TEMP_NUMBER; i++)
fprintf(fp, " %d %d\n", i, td->temps[i]);
fprintf(fp, "Maximum Temperature for the day: %d Degrees F\n", td->max);
fprintf(fp, "Minimum Temperature for the day: %d Degrees F\n", td->min);
fprintf(fp, "Average Temperature for the day: %.1f Degrees F\n", td->avg);
fclose(fp);
}
void calc_results(struct temp_data *td)
{
// do only calculations
td->avg = td->sum / TEMP_NUMBER;
td->min = td->temps[0];
td->max = td->temps[0];
for (int i=1; i < TEMP_NUMBER; i++) {
td->min = td->temps[i] < td->min ? td->temps[i] : td->min;
td->max = td->temps[i] > td->max ? td->temps[i] : td->max;
}
}
int main(int argc, char *argv[])
{
// Moved user input into the main() from read_temps()
// now read_temps() only needs to read and record the temp data
struct temp_data *td = temps_init();
printf("Enter .txt file name \n");
scanf("%123s", td->fname);
strcat(td->fname, ".txt");
read_temps(td);
calc_results(td);
print_results(td);
free(td);
return 0;
};
Using a file called sample.txt:
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
So I'm making a ghetto weather report by creating a random number generator anywhere from 60-100 and storing 25 of these in an array. Then I have a function that calculates max, min, and average along with printing all of this out.
I got it to run without error, but all I'm getting are a bunch of zeros in my display, which means I'm messing up big time somewhere in the calculation, any suggestions?
Also I'm trying to get down calling user-defined functions which is why I have several.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0;
float avg = 0;
int main () {
srand( (unsigned) time(NULL) );
for (i=0; i < 25; i++) {
get_value(i);
sum += temp[i];
}
calc_results(temp[25]);
return 0;
};
int get_value(void) {
return((rand() % (100 - 60 + 1)) + 60);
};
int calc_results(int temp_number[], int number) {
avg = ((sum)/(25));
max = temp[0];
for(i=1;i<25;i++){
if(max<temp[i])
max=temp[i];
};
min =temp[0];
for(i=1;i<25;i++){
if(min>temp[i])
min=temp[i];
};
printf("Temperature Conditions on October 9, 2015 : \n");
printf("Time of day Temperature in degrees F \n");
printf(" 0 %d\n",temp[0]);
printf(" 1 %d\n",temp[1]);
printf(" 2 %d\n",temp[2]);
printf(" 3 %d\n",temp[3]);
printf(" 4 %d\n",temp[4]);
printf(" 5 %d\n",temp[5]);
printf(" 6 %d\n",temp[6]);
printf(" 7 %d\n",temp[7]);
printf(" 8 %d\n",temp[8]);
printf(" 9 %d\n",temp[9]);
printf(" 10 %d\n",temp[10]);
printf(" 11 %d\n",temp[11]);
printf(" 12 %d\n",temp[12]);
printf(" 13 %d\n",temp[13]);
printf(" 14 %d\n",temp[14]);
printf(" 15 %d\n",temp[15]);
printf(" 16 %d\n",temp[16]);
printf(" 17 %d\n",temp[17]);
printf(" 18 %d\n",temp[18]);
printf(" 19 %d\n",temp[19]);
printf(" 20 %d\n",temp[20]);
printf(" 21 %d\n",temp[21]);
printf(" 22 %d\n",temp[22]);
printf(" 23 %d\n",temp[23]);
printf(" 24 %d\n",temp[24]);
printf(" 25 %d\n",temp[25]);
printf("Maximum Temperature for the day: %d Degrees F\n", max);
printf("Minimum Temperature for the day: %d Degrees F\n", min);
printf("Average Temperature for the day: %.1f Degrees F\n", avg);
};
firstly dude use loop to print 25 printf statements... ur code would be a bit smaller...
do the following changes and it should work pretty okay...
time_t t;
srand((unsigned) time(&t)); // this is a more standard way of using srand
also you have passed and int in get_value() whose parameter is void...
do this in the for loop in main
temp[i]=get_value();
also declare your functions on above of your code...
you dont need calc_results() like this...
do it
void calc_results(void)
and no neeed of passing temp since its already global...no need of passing number type integer since you are not using any such thing...no need of using return type as int since you dont need to return any integer...
final suggestion... Get a good book on functions
so your final code would look something like this:-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int get_value(void);
void calc_results(void);
int sum = 0;
int min = 0;
int max = 0;
int temp[25];
int i = 0;
float avg = 0;
int main () {
time_t t;
srand((unsigned) time(&t));
for (i=0; i < 25; i++) {
temp[i]=get_value();
sum += temp[i];
}
calc_results();
return 0;
};
int get_value(void) {
return((rand() % (100 - 60 + 1)) + 60);
};
void calc_results(void) {
avg = ((sum)/(25));
max = temp[0];
for(i=1;i<25;i++){
if(max<temp[i])
max=temp[i];
};
min =temp[0];
for(i=1;i<25;i++){
if(min>temp[i])
min=temp[i];
};
printf("Temperature Conditions on October 9, 2015 : \n");
printf("Time of day Temperature in degrees F \n");
for(int j=0;j<25;j++){
printf(" %d %d\n",i,temp[i]);
}
printf("Maximum Temperature for the day: %d Degrees F\n", max);
printf("Minimum Temperature for the day: %d Degrees F\n", min);
printf("Average Temperature for the day: %.1f Degrees F\n", avg);
};
also Dont use global variables unless local variables fail...this would be at great help when you do some big codes and file handling
Just chance this line
get_value(i);
To
temp[i]=get_value(i);
Because you have to store the random values in temp[i] then you can calculate the other values.
And while you passing the array. You should do this--
calc_results(temp);
You can print all array value by a for loop. Like this:
int cnt;
for(cnt = 0; cnt<=25; cnt++)
{
printf(" %d %d\n",cnt,temp[cnt]);
}
You aren't assigning temp[i] any value?
for (i=0; i < 25; i++) {
temp[i] = get_value(i);
sum += temp[i];
}
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;
}