Monash University > CSSE > CSE1303 > Part A > Lectures > Lecture A01 notes

CSE1303 Computer Science
Semester 2 2003
Part A
Lecture A01 notes: Overview and Revision

In this lecture

Reading: Overview Basic data types in C Boolean Variables Functions Arrays Input/Output Strings File Input/Output Structs Use of typedef

Code included in this lecture

#include <stdio.h>
#include <stbool.h>

int dayTable[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
                        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} };

/* If year is a leap year return true, else return false. */
bool leapYear(int year)
{
 if ((year%4 == 0 && year%100 != 0) || year%400 == 0)
 }
    return true;
 }
 else
 {
    return false;
 }
}

/* Set day of year from month and day. */
int dayOfYear(int year, int month, int day)
{
   int i;
   int isLeap = leapYear(year);

   for (i = 1; i < month; i++)
   {
       day += dayTable[isLeap][i];
   }
   return day;
}

int main()
{
  int year;
  int month;
  int day;

  printf("Enter year, month and day: ");
  scanf("%d %d %d", &year, &month, &day);

  day = dayOfYear(year, month, day);
  printf("\nDay of Year = %d\n", day);
}

#include <stdio.h>
#include <stbool.h>
#define MAXSTRING 100

char name[MAXSTRING];

int main()
{
	bool flag = true;
	while (flag != false)
	{
		if( scanf("%s", name) == 1)
		   printf("Hello %s", name);
		else
		   flag=false;
	}
}

#include <stdio.h>

int main()
{
  int day;
  int month;
  int year;
  char name[30];
 
  printf("Enter your name:\n">
  scanf("%s", name);

  /* skipping spaces */
  printf("Hi %s. Enter birthdate as: dd mm yyyy\n", name);
  scanf("%d %d %d", &day, &month, &year);

  /* alternative */
  printf("Hi %s.  Enter birthdate as: dd-mm-yyyy\n", name);
  scanf("%d-%d-%d", &day, &month, &year);

  return 0;
}

#include <stdio.h>
#include <string.h>

#define MAXLENGTH 100

int main()
{
   char string1[MAXLENGTH];
   char string2[MAXLENGTH];

   strcpy(string1, "Hello World!");
   strcpy(string2, string1);

   length = strlen(string1);
   printf("length of string1 = %d\n", length);

   strcpy(string1, "Apple");
   strcpy(string2, "Orange");

   if (strcmp(string1, string2) < 0)
   { 
     printf("%s %s\n", string1, string2);
   }   
   else if (strcmp(string1, string2) == 0)
   {   
     printf("The strings are the same.\n");
   }   
   else
   {
     printf("%s %s\n", string2, string1);
   }

   strcat(string1, " juice");
   printf("%s\n", string1);

   return 0;   

}

#include <stdio.h>
#define MAXLEN 100
 
int main()
{
  FILE  *inputfile = NULL;
  FILE  *outputfile = NULL;
  char name[MAXLEN];
  int count;
  float mark;
  
  inputfile = fopen("Names.txt", "r");
  outputfile = fopen("marks.dat", "w");

  if (inputfile == NULL)
  {
    printf("Unable to open input file.\n");
    return 1;
  }
  if (outputfile == NULL)
  {
    printf("Unable to open output file.\n");
    return 1;
  }

  count = 0;
  while ( fscanf(inputfile, "%s", name ) == 1 )
  {
    count++;
    
    printf("Enter mark for %s: \n", name);
    scanf("%f", &mark);
    
    if ( fprintf(outputfile, "%s %f\n", name, mark) <= 0 )
    {
      printf("Error writing to output file.\n");
      return 1;
    }
  }

  printf("\n");
  printf("Number of names read: %d\n", count);
  
  fclose(inputfile);
  fclose(outputfile);
  
  return 0;
}

#include <stdio.h>
#define MAXLEN 100 

int main()
{
  FILE  *inputfile = NULL;
  char line[MAXLEN];
  int count = 0;
  
  inputfile = fopen("Names.txt", "r");
  if (inputfile == NULL)
  {
    printf("Unable to open input file.\n");
    return 1;
  }

   while(fgets(line, MAXLEN, inputfile) != NULL)
   {
		count++;
   }
  
   printf("Number of lines: %d", count);
   fclose(inputfile);
   return 0;
}  

[ Top | Home ]
Last Updated: Thursday 26 June 2003 10:59:03