Home
Diploma Practice C Online People
Certificate Contact Us

Solutions for assignments on advanced functions

as01.c

#include <stdio.h>

int getLargestNum(int *nums, int count)
{
  int largeNum;
  int ii;

  largeNum = nums[0];
  for(ii=1; ii<count; ii++)
  {
    if(nums[ii] > largeNum)
      largeNum = nums[ii];
  }
  return largeNum;
}

int main()
{
  int array[5];
  int ii;
  int large;

  printf("Enter the numbers to fill the array of 5 integers\n");
  for(ii=0; ii<5; ii++)
  {
    printf("Enter number %d : ",ii+1);
    scanf("%d",&array[ii]);
  }
  large = getLargestNum(array, 5);
  printf("Largest number is %d\n", large);
}

as02.c

#include <stdio.h>

int main()
{
  int array[100];
  int ii;
  int large;
  int count;

  printf("How many numbers to you wish to enter: ");
  do
  {
    scanf("%d",&count);
    if(count > 100)
    {
      printf("Count should be less than or equal to 100, try again: ");
    }
  }while(count > 100);
 
  for(ii=0; ii<count; ii++)
  {
    printf("Enter number %d : ",ii+1);
    scanf("%d",&array[ii]);
  }
  large = getLargestNum(array, count);
  printf("Largest number is %d\n", large);
}

as03.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
  int *array;
  int ii;
  int large;
  int count;

  printf("How many numbers to you wish to enter: ");
  scanf("%d",&count);
  array = malloc(count * sizeof(int)); 
  for(ii=0; ii<count; ii++)
  {
    printf("Enter number %d : ",ii+1);
    scanf("%d",&array[ii]);
  }
  large = getLargestNum(array, count);
  printf("Largest number is %d\n", large);
}

as04.c

#include <stdio.h>

int getCountOfCapitalLetters(char *str)
{
  int capcount = 0;

  while(*str)
  {
    if(*str >= 'A' && *str <= 'Z')
      capcount++;
    str++;
  }
  return capcount;
}

int main()
{
  int  count;
  char lineOfText[128];

  printf("Enter a line of text\n");
  fgets(lineOfText,128,stdin);

  count = getCountOfCapitalLetters(lineOfText);
  printf("The entered line contains %d capital letters\n", count);
}

as05.c

#include <stdio.h>

void convertToLowerCase(char *str)
{
  while(*str)
  {
    if(*str >= 'A' && *str <= 'Z')
      *str += 0x20;
    str++;
  }
}

int main()
{
  char lineOfText[128];

  printf("Enter a line of text to convert to lower case\n");
  fgets(lineOfText,128,stdin);

  convertToLowerCase(lineOfText);
  printf("Line after conversion: %s\n", lineOfText);
}

as06.c

#include <stdio.h>

void strcpyLowerCase(char *dst, char *src)
{
  while(*src)
  {
    if(*src >= 'A' && *src <= 'Z')
      *dst = *src + 0x20;
    else
      *dst = *src;
    src++;
    dst++;
  }
  *dst = *src;
}

int main()
{
  char line1[128];
  char line2[128];

  printf("Enter a line of text to convert to lower case\n");
  fgets(line1,128,stdin);

  strcpyLowerCase(line2, line1);
  printf("Line 1 : %s", line1);
  printf("Line 2 : %s", line2);
}

as07.c

#include <stdio.h>

#define SEC_PER_HOUR  3600
#define SEC_PER_MIN   60

struct Time
{
  int hour;
  int min;
  int sec;
};

int getSecondsFrmTime(struct Time *tm)
{
  int seconds;

  seconds = (tm->hour * SEC_PER_HOUR) + (tm->hour * SEC_PER_MIN) + tm->sec;
  return seconds;
}

int main()
{
  struct Time tm;
  int sec;

  printf("Enter hours, minutes and seconds with spaces in between: ");
  scanf("%d%d%d", &tm.hour, &tm.min, &tm.sec);
  sec = getSecondsFrmTime(&tm);
  printf("Number of seconds in %02d:%02d:%02d are %d\n", tm.hour, 
                       tm.min, tm.sec, sec);
}

as08.c

#include <stdio.h>

#define SEC_PER_HOUR  3600
#define SEC_PER_MIN   60

struct Time
{
  int hour;
  int min;
  int sec;
};


void convertSecondsToTime(int seconds, struct Time *tm)
{
  tm->hour = seconds / SEC_PER_HOUR;
  seconds  = seconds % SEC_PER_HOUR;
  tm->min  = seconds / SEC_PER_MIN;
  tm->sec  = seconds % SEC_PER_MIN;
}

int main()
{
  int sec;
  struct Time tm;

  printf("Enter number of seconds: "); 
  scanf("%d", &sec);
  convertSecondsToTime(sec, &tm);
  printf("%d seconds correspond to time: %02d:%02d:%02d\n", 
                                         sec, tm.hour, tm.min, tm.sec);
}

as09.c

#include <stdio.h>

struct Date
{
  char  date;
  char  month;
  short year;
};

struct Student
{
  char  name[40];
  long long mobile;
  struct Date dob; //Date of birth, struct Date as given in assignment 2.
};

void getStudentInfo(struct Student *ps)
{
  printf("Enter students name: ");
  gets(ps->name);
  printf("Enter students mobile number: ");
  scanf("%lld", &ps->mobile);
  printf("Enter students date of birth as DD MM YYYY: ");
  scanf("%hhd%hhd%hd", &ps->dob.date, &ps->dob.month, &ps->dob.year);
}

int main()
{
  struct Student stu;

  getStudentInfo(&stu);
  printf("Student Name  : %s\n",stu.name);
  printf("Student mobile: %lld\n",stu.mobile);
  printf("Student DOB   : %02d/%02d/%04d\n", stu.dob.date,
                   stu.dob.month, stu.dob.year);
}

as10.c

#include <stdio.h>

struct Date
{
  char  date;
  char  month;
  short year;
};

struct Student
{
  char  name[40];
  long long mobile;
  struct Date dob; //Date of birth, struct Date as given in assignment 2.
};

void getStudentInfo(struct Student *ps)
{
  printf("Enter students name: ");
  gets(ps->name);
  printf("Enter students mobile number: ");
  scanf("%lld", &ps->mobile);
  printf("Enter students date of birth as DD MM YYYY: ");
  scanf("%hhd%hhd%hd", &ps->dob.date, &ps->dob.month, &ps->dob.year);
}

void printStudentInfo(struct Student *ps)
{
  printf("Following are student details:\n");
  printf("  Name  : %s\n",ps->name);
  printf("  mobile: %lld\n",ps->mobile);
  printf("  DOB   : %02d/%02d/%04d\n", ps->dob.date,
                   ps->dob.month, ps->dob.year);
}

int main()
{
  struct Student stu;

  getStudentInfo(&stu);
  printStudentInfo(&stu);
}

as11.c

#include <stdio.h>

#define MAX_SUBJECTS 6

void getTotalAvgMaxMin(int *marks, int *tot, int *avg, int *max, int *min)
{
  int ii;
  int minMark, maxMark, totalMarks; 

  minMark    = marks[0];
  maxMark    = marks[0];
  totalMarks = marks[0];

  for(ii=1; ii<MAX_SUBJECTS; ii++)
  {
    if(marks[ii] < minMark)
      minMark = marks[ii];
    if(marks[ii] > maxMark)
      maxMark = marks[ii];
    totalMarks += marks[ii];
  }

  *tot = totalMarks;
  *avg = totalMarks/MAX_SUBJECTS;
  *min = minMark;
  *max = maxMark;
}

int main()
{
  int marks[MAX_SUBJECTS];
  int ii, max, min, total, average;

  for(ii=0; ii<MAX_SUBJECTS; ii++)
  {
    printf("Enter marks of subject %d: ", ii+1);
    scanf("%d", &marks[ii]);
  }

  getTotalAvgMaxMin(marks, &total, &average, &max, &min);

  printf("Total Marks  : %d\n", total);
  printf("Average Mark : %d\n", average);
  printf("Highest Mark : %d\n", max);
  printf("Lowest Mark  : %d\n", min);
}

as12.c

#include <stdio.h>

#define MAX_SUBJECTS 6

void getTotalAvgMaxMin(int *marks, int *res)
{
  int ii;
  int minMark, maxMark, totalMarks; 

  minMark    = marks[0];
  maxMark    = marks[0];
  totalMarks = marks[0];

  for(ii=1; ii<MAX_SUBJECTS; ii++)
  {
    if(marks[ii] < minMark)
      minMark = marks[ii];
    if(marks[ii] > maxMark)
      maxMark = marks[ii];
    totalMarks += marks[ii];
  }

  res[0] = totalMarks;
  res[1] = totalMarks/MAX_SUBJECTS;
  res[2] = maxMark;
  res[3] = minMark;
}

int main()
{
  int marks[MAX_SUBJECTS];
  int result[4];
  int ii;

  for(ii=0; ii<MAX_SUBJECTS; ii++)
  {
    printf("Enter marks of subject %d: ", ii+1);
    scanf("%d", &marks[ii]);
  }

  getTotalAvgMaxMin(marks, result);

  printf("Total Marks  : %d\n", result[0]);
  printf("Average Mark : %d\n", result[1]);
  printf("Highest Mark : %d\n", result[2]);
  printf("Lowest Mark  : %d\n", result[3]);
}


#401, Sai Sushma Homes, 23/A, S.R. Nagar Main Road, Hyderabad-500038, India.
Ph: +91-40-48508764, 7702028989, depik.help@gmail.com