Compare commits
No commits in common. "dd75cd75e74217ba004af546bae987086cac2212" and "ea84eccf12226ed1c1ced2f9d60bbf0c6e7a62dc" have entirely different histories.
dd75cd75e7
...
ea84eccf12
@ -1,125 +0,0 @@
|
||||
// Modified from C18 Ex5
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define DEBUG_MAIN
|
||||
#define DEBUG_FNS
|
||||
|
||||
|
||||
float degrees_to_radians(float degrees) {
|
||||
#ifdef DEBUG_FNS
|
||||
printf("degrees_to_radians(degrees=%f): called\n", degrees);
|
||||
#endif
|
||||
return M_PI * degrees / 180.0;
|
||||
}
|
||||
|
||||
|
||||
// Simple function to populate an integer array
|
||||
void PopulateTheArray ( int Size, float ArrayData[])
|
||||
{
|
||||
#ifdef DEBUG_FNS
|
||||
printf("PopulateTheArray(Size=%d, ArrayData): called\n", Size);
|
||||
#endif
|
||||
int i; // Variable to use in our loop
|
||||
|
||||
for ( i = 0 ; i < Size ; i++)
|
||||
{
|
||||
ArrayData[i] = degrees_to_radians(i); // Treat it like a normal array
|
||||
}
|
||||
}
|
||||
// Simple function do display contents an integer array
|
||||
void DisplayTheArray ( int Size, float ArrayData[])
|
||||
{
|
||||
#ifdef DEBUG_FNS
|
||||
printf("DisplayTheArray(Size=%d, ArrayData): called\n", Size);
|
||||
#endif
|
||||
int i; // Variable to use in our loop
|
||||
|
||||
for ( i = 0 ; i < Size ; i++)
|
||||
{
|
||||
#ifdef DEBUG_FNS
|
||||
printf("DisplayTheArray(): i=%d\n", i);
|
||||
#endif
|
||||
printf ("%d\t%0.3f\n", i, ArrayData[i]);
|
||||
}
|
||||
}
|
||||
|
||||
int SaveArrayBinary (char *filename, int Size, float ArrayData[])
|
||||
{
|
||||
#ifdef DEBUG_FNS
|
||||
printf("DisplayTheArray(filename=%s, Size=%d, ArrayData): called\n", filename, Size);
|
||||
#endif
|
||||
FILE *fp; // File Pointer
|
||||
int i; // Variable to use in our loop
|
||||
|
||||
fp = fopen(filename, "wb");
|
||||
#ifdef DEBUG_FNS
|
||||
printf("DisplayTheArray(): fp=%p\n", fp);
|
||||
#endif
|
||||
// if file open failed, return error to caller
|
||||
if (fp == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// write data
|
||||
fwrite(ArrayData, sizeof(float), Size, fp);
|
||||
// close file, returns 0 on successful closing (therefore function has executed sucessfully)
|
||||
return fclose(fp);
|
||||
}
|
||||
// Main () - execution starts here
|
||||
int main (void)
|
||||
{
|
||||
#ifdef DEBUG_MAIN
|
||||
printf("main(): called\n");
|
||||
#endif
|
||||
char filename[2049];
|
||||
int rc, iSizeForArray;
|
||||
float *pData; // A pointer to hold the base address of out array
|
||||
|
||||
// Ask for the size of the array and store result
|
||||
|
||||
printf("Please enter the size of the array to dynamically allocate: ");
|
||||
scanf ("%d", &iSizeForArray);
|
||||
#ifdef DEBUG_MAIN
|
||||
printf("main(): iSizeForArray=%d\n", iSizeForArray);
|
||||
#endif
|
||||
|
||||
// Use calloc with checking
|
||||
pData = calloc( iSizeForArray, sizeof (float));
|
||||
#ifdef DEBUG_MAIN
|
||||
printf("main(): pData=%p\n", pData);
|
||||
#endif
|
||||
|
||||
// Check we got the memory
|
||||
if ( pData == NULL)
|
||||
{
|
||||
printf ("Sorry, I could not allocate the memory, bye!\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Pass the size, iSizeForArray) and the pointer created
|
||||
// which points to the start of the sucesfully allocated memory
|
||||
|
||||
PopulateTheArray(iSizeForArray, pData);
|
||||
DisplayTheArray(iSizeForArray, pData);
|
||||
|
||||
// Exercise doesn't ask to use function but kind of pointless without
|
||||
printf("Enter filename: ");
|
||||
scanf("%2048s", filename);
|
||||
#ifdef DEBUG_MAIN
|
||||
printf("main(): filename=%s\n", filename);
|
||||
#endif
|
||||
rc = SaveArrayBinary(filename, iSizeForArray, pData);
|
||||
#ifdef DEBUG_MAIN
|
||||
printf("main(): rc=%d\n", rc);
|
||||
#endif
|
||||
if (rc != 0) {
|
||||
printf("Error saving file\n");
|
||||
free(pData);
|
||||
return rc;
|
||||
}
|
||||
|
||||
free(pData); // Free up the memory before exiting
|
||||
return (0); // Exit indicating sucess
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int age;
|
||||
char name[10];
|
||||
|
||||
if ( argc != 3 )
|
||||
{
|
||||
printf ("USAGE: %s name age\n",argv[0]);
|
||||
exit (0);
|
||||
}
|
||||
|
||||
sscanf(argv[1],"%s",name);
|
||||
sscanf(argv[2],"%d",&age);
|
||||
|
||||
printf ("age=%d name=%s\n", age, name);
|
||||
|
||||
return 0;
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
// Modified from C10 Ex5
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int date_to_day(int year, int month, int day) {
|
||||
if (month < 3) {
|
||||
month += 12;
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7;
|
||||
}
|
||||
|
||||
void print_day_of_week(int day) {
|
||||
switch (day) {
|
||||
case 0: printf("Monday\n"); break;
|
||||
case 1: printf("Tuesday\n"); break;
|
||||
case 2: printf("Wednesday\n"); break;
|
||||
case 3: printf("Thursday\n"); break;
|
||||
case 4: printf("Friday\n"); break;
|
||||
case 5: printf("Saturday\n"); break;
|
||||
case 6: printf("Sunday\n"); break;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
int rc, year, month, day, nd;
|
||||
|
||||
if ( argc != 4 )
|
||||
{
|
||||
printf ("USAGE: %s year month day\n",argv[0]);
|
||||
exit (0);
|
||||
}
|
||||
|
||||
sscanf(argv[1],"%d",&year);
|
||||
sscanf(argv[2],"%d",&month);
|
||||
sscanf(argv[3],"%d",&day);
|
||||
|
||||
nd = date_to_day(year, month, day);
|
||||
print_day_of_week(nd);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user