Wednesday, 23 April 2014

Write an interactive program to generate progress reports for the students of class XII (Science group). Assumptions can be made wherever necessary.

#include<stdio.h>
#include<conio.h>

#define STU_NUM 3
#define NAME_LEN 100
#define SUB_NUM 3

struct student
{
    int id;
    char name[NAME_LEN];
    char group;
    int subject_marks[SUB_NUM];
    int total;
    int percentage;
};

void main()
{
    int i, j, k, l, total, percentage;
    struct student stud[STU_NUM];
    clrscr();

    //Get the required data from user.
    for(i=0; i<STU_NUM; i++)
    {
        j = i+1;

        printf("\n\nEnter the ID of student %d: ",j);
        scanf("%d",&stud[i].id);
        fflush(stdin);
       
        printf("\nEnter the name of student %d: ",j);
        gets(stud[i].name);
        fflush(stdin);
       
        printf("\nEnter the group selected by student %d (A/B): ",j);
        scanf("%c",&stud[i].group);
        fflush(stdin);
       
        for(k=0; k<SUB_NUM; k++)
        {
            l = k+1;
            printf("\nEnter the marks of subject %d for student %d: ",l,j);
            scanf("%d",&stud[i].subject_marks[k]);
        }
        fflush(stdin);
    }
   
    //Calculate total & percentage of the student.
    for(i=0; i<STU_NUM; i++)
    {
        total = 0;
        percentage = 0;
        for(k=0; k<SUB_NUM; k++)
        {
            total = total + stud[i].subject_marks[k];
        }
        percentage = total / SUB_NUM;
       
        stud[i].total = total;
        stud[i].percentage = percentage;
    }
   
    //Display the progress report of the students.
    clrscr();
    for(i=0; i<STU_NUM; i++)
    {
        printf("ID: %d \t Name: %s \t Group: %c \n", stud[i].id, stud[i].name, stud[i].group);
       
        for(k=0; k<SUB_NUM; k++)
        {
            l = k+1;
            printf("Subject %d: %d / 100 \n", l, stud[i].subject_marks[k]);
        }
        printf("Total: %d \t Percentage: %d \n", stud[i].total, stud[i].percentage);
        printf("\n----------------------------------------------------------------------\n\n");
    }

    getch();
}

Write an interactive program called “DISTANCE CONVERTER” that accepts the distance in millimetres / feet / miles / yards / kilometres and displays its equivalent in metres.

#include<stdio.h>
#include<conio.h>

void main()
{
    float distance_unit, distance_meter;
    int choice, cont = 1;
    clrscr();

    printf("\nDistance Converter\n");

    while(cont == 1)
    {
        clrscr();
        printf("\n1.Enter distance in Millimeters.\n");
        printf("\n2.Enter distance in Feet.\n");
        printf("\n3.Enter distance in Miles.\n");
        printf("\n4.Enter distance in Yards.\n");
        printf("\n5.Enter distance in Kilometers.\n");
       
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
       
        printf("\nEnter distance: ");
        scanf("%f", &distance_unit);
       
        switch(choice)
        {
            case 1:
                //1 meter = 1000 mm
                distance_meter = distance_unit / 1000;
                break;
            case 2:
                //1 meter = 3.28 feet
                distance_meter = distance_unit / 3.28;
                break;
            case 3:
                //1 mile = 1609 meter
                distance_meter = distance_unit * 1609;
                break;
            case 4:
                //1 meter = 1.0936 yard
                distance_meter = distance_unit / 1.0936;
                break;
            case 5:
                //1 kilometer = 1000 meter
                distance_meter = distance_unit * 1000;
                break;
            default:
                printf("\nInvalid choice selected.\n");
                break;
        }
       
        printf("\nEquivalent distance in Metres is: %f\n", distance_meter);
       
        printf("\nDo you want to continue (1/0)? ");
        scanf("%d", &cont);
    }
   
    getch();
}

Declare two arrays A and B, find (i) A intersection B and (ii) A union B.

#include<stdio.h>
#include<conio.h>
#define SIZE 5
void get_data(int arr[]);
void print_data(int arr[], int n);
void bubble_sort(int arr[]);
int find_intersection(int array_1[], int array_2[], int intersect_result[]);
int find_union(int array_1[], int array_2[], int union_result[]);
void main()
{
int array_1[SIZE], array_2[SIZE], intersect_result[SIZE], union_result[SIZE*2];
int num_elements;
clrscr();
//Get the elements of Array1
printf("\nEnter the elements of Array 1: \n");
get_data(array_1);
printf("\n\nElements of Array 1: ");
print_data(array_1, SIZE);
//Sort array 1
bubble_sort(array_1);
printf("\n\nSorted elements of Array 1: ");
print_data(array_1, SIZE);
//Get the elements of Array2
printf("\n\nEnter the elements of Array 2: \n");
get_data(array_2);
printf("\n\nElements of Array 2: ");
print_data(array_2, SIZE);
//Sort array 2
bubble_sort(array_2);
printf("\n\nSorted elements of Array 2: ");
print_data(array_2, SIZE);
//Find Intersection and print the result
num_elements = find_intersection(array_1, array_2, intersect_result);
printf("\n\nIntersection is: ");
print_data(intersect_result, num_elements);
//Find Union
num_elements = find_union(array_1, array_2, union_result);
printf("\n\nUnion is: ");
print_data(union_result, num_elements);
getch();
}
void get_data(int arr[])
{
int i,j;
for(i=0; i<SIZE; i++)
{
j = i+1;
printf("\nEnter element %d: ",j);
scanf("%d", &arr[i]);
}
}
void print_data(int arr[], int n)
{
int i; printf("{ ");
for(i=0; i<n; i++)
{
printf("%d ",arr[i]);
}
printf("}");
}
void bubble_sort(int arr[])
{
int i,j,temp,swapped;
for(i=1; i<SIZE; i++)
{
swapped = 0;
for(j=0; j<SIZE-i; j++)
{
if(arr[j] > arr[j+1])
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
swapped = 1;
}
}
if(swapped == 0)
{
break;
}
}
}
int find_intersection(int array_1[], int array_2[], int intersect_result[])
{
int i = 0, j = 0, k = 0;
while((i<SIZE) && (j<SIZE))
{
if(array_1[i] < array_2[j])
{
i++;
}
else if(array_1[i] > array_2[j])
{
j++;
}
else
{
intersect_result[k] = array_1[i];
i++;
j++;
k++;
}
}
return(k);
}
int find_union(int array_1[], int array_2[], int union_result[])
{
int i = 0, j = 0, k = 0;
while((i<SIZE) && (j<SIZE))
{
if(array_1[i] < array_2[j])
{
union_result[k] = array_1[i];
i++;
k++;
}
else if(array_1[i] > array_2[j])
{
union_result[k] = array_2[j];
j++;
k++;
}
else
{
union_result[k] = array_1[i];
i++;
j++;
k++;
}
}
if(i == SIZE)
{
while(j<SIZE)
{
union_result[k] = array_2[j];
j++;
k++;
}
}
else
{
while(i<SIZE)
{
union_result[k] = array_1[i];
i++;
k++;
}
}
return(k);
}

Declare two arrays A and B, find (i) A intersection B and (ii) A union B.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,num_lines;
clrscr();
printf("\nEnter the number of lines to print: ");
scanf("%d", &num_lines);
for(i=1; i<=num_lines; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ", i);
}
printf("\n");
}
getch();
}

Write a program to print the following pattern: 1 b) 1 1 2 2 2 1 2 3 3 3 3 1 2 3 4 4 4 4 4 1 2 3 4 5 5 5 5 5 5

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,num_lines;
clrscr();
printf("\nEnter the number of lines to print: ");
scanf("%d", &num_lines);
for(i=1; i<=num_lines; i++)
{
for(j=1; j<=i; j++)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}

Write an interactive Cprogram which prompts the user with the following options on the opening menu: (40 Marks) 1) Study Centre Information 2) Details of the programmes activated in the study centre 3) Scheduling of theory/practical sessions for BCA programee 4) Academic Councillor Details 5) Assignments submission and viva voce schedule 7) Quit Enter your choice: If an “1” is entered, prompt the user to enter the study centre code and know the details about the study like name of the study centre, name of the regional centre, name of the study centre coordinator, programme in-charge details etc. If “2” is entered, it should give the details of all the programmes those are activated in the centre. If “3” is entered, it should give the schedule for the theory and practical counselling sessions for BCA programme for the current session. If “4” is entered it should display the details of the academic councillors’ associated with respective programmes. If “5” is entered it should display the assignments submission and viva voce schedule for the current session. If the user enters any letters or numbers other than the choice, redisplay the prompt. All output should go to the terminal and all input should come from the keyboard. Note: You must execute the program and submit the program logic, sample input and output along with the necessary documentation for this practical question. Assumptions can be made wherever necessary.

#include<conio.h>
#include<stdio.h>
struct bcastudent
{
int stcode;
char scname[50];
char scaddr[300];
char rcname[50];
char sccoor[250];
char icname[250];
char progac[7];
char thdate[25];
char thtime[25];
char prdate[25];
char prtime[25];
char accoun[250];
char assubm[250];
char vivavo[250];
}s;
FILE *f1,*f2;
int i,amount,rol;
char chh[150];
int ch;
int b,p;
addrecord()
{
f1=fopen("data1","ab");
fflush(stdin);
printf("\nEnter the Study Center Code                = ");
scanf("%d",&s.stcode);
fflush(stdin);
printf("\nEnter the Name of the Study Center            = ");
scanf("%s",s.scname);
fflush(stdin);
printf("\nEnter the Address of the Study Center            = ");
scanf("%s",s.scaddr);
fflush(stdin);
printf("\nEnter the Name of the Regional Center            = ");
scanf("%s",&s.rcname);
fflush(stdin);
printf("\nEnter the Name of the Study Center Coordinator        = ");
scanf("%s",&s.sccoor);
fflush(stdin);
printf("\nEnter the Name of the Program In-Charge            = ");
scanf("%s",&s.icname);
fflush(stdin);
printf("\nEnter the Details of Programs Activated in the Center    = ");
scanf("%s",s.progac);
fflush(stdin);
printf("\nEnter the Date for Theory Counseling");
printf("\nSession for BCA Program (DD/MM/YYYY)            = ");
scanf("%s",s.thdate);
fflush(stdin);
printf("\nEnter the Time for Theory Counseling");
printf("\nSession for BCA Program (00:00 AM/PM)            = ");
scanf("%s",s.thtime);
fflush(stdin);
printf("\nEnter the Date for Practical Counseling");
printf("\nSession for BCA Program (DD/MM/YYYY)            = ");
scanf("%s",s.prdate);
fflush(stdin);
printf("\nEnter the Time for Practical Counseling");
printf("\nSession for BCA Program (00:00 AM/PM)            = ");
scanf("%s",s.prtime);
fflush(stdin);
printf("\nEnter the Details of the Academic Counselors");
printf("\nAssociated with Respective Programs (PROG COUNSELOR)    = ");
scanf("%s",s.accoun);
fflush(stdin);
printf("\nEnter the Assignments Submission Schedule");
printf("\nfor the Current Session (PROGNAME    LASTDATE    = ");
scanf("%s",s.assubm);
fflush(stdin);
printf("\nEnter the Viva Voce Schedule");
printf("\nfor the Current Session (PROGNAME    DATE        = ");
scanf("%s",s.vivavo);
fwrite(&s,sizeof(s),1,f1);
fflush(stdin);
fclose(f1);
return 0;
}
disprecord()
{
f1=fopen("data1","r+b");
fflush(f1);
rewind(f1);
while(fread(&s,sizeof(s),1,f1))
{
printf("\nStudy Center Code=%d\n",s.stcode);
printf("\nName of the Study Center=%s\n",s.scname);
printf("\nAddress of the Study Center=%s\n",s.scaddr);
printf("\nName of the Regional Center=%s\n",s.rcname);
printf("\nName of the Study Center Coordinator=%s\n",s.sccoor);
printf("\nName of the Program In-Charge=%s\n",s.icname);
}
close(f1);
return 0;
}
disprec()
{
f1=fopen("data1","rb+");
fflush(stdin);
printf("Enter the Study Center Code=");
scanf("%d",&rol);
{
if(rol==s.stcode)
{
printf("\n*********************************************************\n");
printf("\nStudy Center Code            =  %d\n",s.stcode);
printf("\nName of the Study Center        =  %s\n",s.scname);
printf("\nAddress of the Study Center        =  %s\n",s.scaddr);
printf("\nName of the Regional Center        =  %s\n",s.rcname);
printf("\nName of the Study Center Coordinator    =  %s\n",s.sccoor);
printf("\nName of the Program In-Charge        =  %s\n",s.icname);
printf("\n*********************************************************\n");
}
}
close(f1);
return 0;
}
dispactiv()
{
f1=fopen("data1","rb+");
fflush(stdin);
printf("Enter the Study Center Code=");
scanf("%d",&b);
{
if(b==s.stcode)
{
printf("\nDetails of the Programs Activated in Study Center = CMR");
printf("\n                                                  = %s\n",s.progac); 
}
}
close(f1);
return 0;
}
dispsched()
{
f1=fopen("data1","rb+");
fflush(stdin);
printf("Enter the Study Center Code=");
scanf("%d",&p);
{
if(p==s.stcode)
{
printf("\nDate for Theory Counseling Session for BCA Program    =  %s\n",s.thdate);
printf("\nTime for Theory Counseling Session for BCA Program    =  %s\n",s.thtime);
printf("\nDate for Practical Counseling Session for BCA Program    =  %s\n",s.prdate);
printf("\nTime for Practical Counseling Session for BCA Program    =  %s\n",s.prtime);
}
}
close(f1);
return 0;
}
dispacco()
{
f1=fopen("data1","rb+");
fflush(stdin);
printf("Enter the Study Center Code=");
scanf("%d",&p);
{
if(p==s.stcode)
{
printf("\nDetails of the Academic Counselors Associated with Respective Programs\n");
printf("\n               PROGRAM NAME              COUNSELOR NAME               \n");
printf("\n                   BCA                        MISHRA                  \n");
}
}
close(f1);
return 0;
}
dispsubm()
{
f1=fopen("data1","rb+");
fflush(stdin);
printf("Enter the Study Center Code=");
scanf("%d",&p);
{
if(p==s.stcode)
{
printf("\n*******************************************************\n");
printf("\nAssignments Submission Schedule for the Current Session\n");
printf("\n*******************************************************\n");
printf("\n                                                       \n");
printf("\n           PROGRAM NAMES          LAST DATES           \n");
printf("\n                BCA               15/04/2013           \n");
printf("\n                                                       \n");
printf("\n                                                       \n");
printf("\n      Viva Voce Schedule for the Current Session       \n");
printf("\n*******************************************************\n");
printf("\n                                                       \n");
printf("\n               PROGRAM NAME        DATE                \n");
printf("\n                    BCA          15/06/2013            \n");
printf("\n*******************************************************\n");
}
}
close(f1);
return 0;
}
main()
{
char chh='y';
i=sizeof(s);
do
{
clrscr();
printf("\n\t ********Classes for BCA & MCA from IGNOU*******\n");

printf("\n***************************************************************************\n");
printf("\n\n To Add Study Center Details                    PRESS 0");
printf("\n\n To Display Study Centre Information                PRESS 1");
printf("\n\n For Details of the programmes Activated in the Study Center    PRESS 2");
printf("\n\n For Scheduling of Theory/Practical Sessions for BCA programmes    PRESS 3");
printf("\n\n For Academic Councillor Details                PRESS 4");
printf("\n\n For Assignments Submission and Viva Voce Schedule        PRESS 5");
printf("\n\n To Quit                            PRESS 6");
printf("\n\n*************************************************************************\n");
printf("\n\n Enter your choice                    :");
scanf("%d",&ch);
switch(ch)
{
case 0:
addrecord();
break;
case 1:
disprec();
break;
case 2:
dispactiv();
break;
case 3:
dispsched();
break;
case 4:
dispacco();
break;
case 5:
dispsubm();
break;
case 6:
exit(0);
break;
default:
printf("\wrong choice");
}
printf("\n\nDo you want to continue(Y/N)");
chh=getche();
}while(chh=='Y');
getch();
return 0;
}


33. Modify the program no: 23 using file concept.



#include<stdio.h>
#include<conio.h> main()
{
char *str; int len,i; FILE *fp; clrscr();
fp=fopen("vim","r");
fscanf(fp,"%s",str); len=strlen(str); for(i=len-1;i>=0;i--)
printf("%c",*(str+i)); getch();
return 0;
}
Output: emoclew

32. Write a program that will input a person’s first name, last name, SSN number and age and write the information to a data file. One person’s information should be in a single line. Use the function fprintfto write to the data file. Accept the information and write the data within a loop. Your program should exit the loop when the word ’EXIT’ is entered for the first name. Remember to close the file before terminating the program. Hint: Use the function strcmpO to compare two strings.



#include <stdio.h>
#include<string.h>
#include<conio.h> main()
{
char fname[50], lname[50]; int ssn, age;
FILE *fp; clrscr();
if ( (fp = fopen("vim","w")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n"); getch();
return 1;
}
do
{ printf("enter the First name:"); scanf("%s",fname); if(strcmp(fname,"EXIT")==0)
{
fclose(fp); exit(1);
}
printf("enter the Last name:"); scanf("%s",lname); printf("enter the SSN:"); scanf("%d",&ssn); printf("enter the age:"); scanf("%d",&age);
fprintf(fp,"%s %s %d %d\n",fname,lname,ssn,age);
} while (1);
}

31. Write a program to create a file, open it, type-in some characters and count the number of characters in a file.



#include <stdio.h>
#include<conio.h>
#include<string.h> main()
{
char ch;
int count=0; FILE *fp; clrscr();
if ( (fp = fopen("vim","w")) == NULL)
{
fprintf(stderr, "Cannot open input file.\n"); getch();
return 1;
}
while ( ( ch =getchar()) != EOF )

{
putc(ch,fp); switch(ch)
{
case \n:
case \t:
case :break; default : count++; break;
}
}
printf("no of characters: %d\n", count); fclose(fp);
getch();
return 0;
}