Thursday, May 14, 2015

Final Question: Structured Programming Language



Final Question: Structured Programming Language 


UNITED INTERNATIONAL UNIVERSITY (UIU)
Dept. Of Electrical & Electronic Engineering (EEE)
Exam: Final, Trimester: Summer, Year: 2010
Course: CSI 121 (Sec – A,B,E), Title: Structured Programming Language
Marks: 60, Time: 2:00 Hour


There are 6 questions. Answer any five. Figures in the right-hand margin indicate full marks. 
 


1.

Write the output of the program below:

 

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

void main()
{
int i;
char str[40] = {‘1’, ‘2’, ‘#’ , ‘  ’,  ‘a’, ‘b’, ‘C’, ‘\0’ };

for( i=0; str[i]; i++ ){
if(isalpha(str[i])){
if(isupper(str[i])) printf( "A");
else printf( "A");
}
else if(isdigit(str[i])) printf( "D");
else if(isspace(str[i])) printf( "_");
else printf( "O");
}
char s1[]= “I am X ”;
char s3[20],  s2[]= “connected”;

strcpy(s3, s1);
printf(“\n%s”,s3);

strcat(s3, s2);
printf(“\n%s”,s3);

strcpy(&s3[5], s2);
printf(“\n%s”,s3);

strcat(s3, &s1[5]);
printf(“\n%s”,s3);
}

[12]




2.

Write the output of the program below:

#include <stdio.h>
#define SIZE 3

struct student {
            int id;
            char *name;
} student_list[SIZE];

int end = -1;

void insert( int, char* );
struct student remove();

void main( void )
{
            insert( 21071063, "Zahid Hasan" );
            insert( 21073010, "Md. Yaqub" );
            insert( 21062076, "Nayan Rahman" );
            insert( 21071003, "Amina Akhter" );

            struct student tmp = remove(); printf( "%d %s\n", tmp.id, tmp.name );

            insert( 21091153, "Mukit-ul Hasan" );

            tmp = remove(); printf( "%d %s\n", tmp.id, tmp.name );
            tmp = remove(); printf( "%d %s\n", tmp.id, tmp.name );
            tmp = remove(); printf( "%d %s\n", tmp.id, tmp.name );
            tmp = remove();
}

void insert( int id, char *name )
{
            if( end + 1 < SIZE ) {
                        end++;
                        for( int i=end; i>0; i-- )
                                    student_list[i] = student_list[i-1];

                        struct student tmp = { id, name };
                        student_list[0] = tmp;
            }
            else
                        printf( "List is full\n" );
}

struct student remove()
{
            if( end >= 0 ) {
                        struct student tmp = student_list[0];
                        for( int i=0; i<end; i++ )
                                    student_list[i] = student_list[i+1];

                        end--;
                        return tmp;
            }
            else
                        printf( "List is empty\n" );
}
[12]




3.

All the questions below are part of a single program.


a.
Define a structure student_info that has two members: id and name where id is an int and name is a string.
[2]

b.
Declare array variable students containing 10 student_info structures.
[2]

c.
Initialize students so that id of 1st entry is 0, id of 2nd entry is 1 and so forth and, name of 1st entry is name0, name of 2nd entry is name1 and so forth.
[2]

d.
Open a file and write all the information in students into the file using fwrite() function.  Make sure to close the file.
[3]

e.
Open the file again and read the info of the 5th student (i.e. student whose id is 4) from the file using fseek() and fread() functions and print it on the screen.  Make sure to close the file.
[3]




4.

Write a C program that reads a paragraph of text from user and then prints the lines separately. Use a function to count and show the number of words in each line.
[12]




5.
a.
Write a C program to read texts from the keyboard and save them into a text file.
[6]

b.
Write a C program to read the contents of a text file and print them on the screen.
[6]






6.



Text Box: struct student {
 int ID; 
 float grade1; int credit1;
 float grade2; int credit2;
 float grade3; int credit3;
 float GPA; 
};

Write a program to get information of 20 students from user and store them in an array of structures defined above. Information includes grades and credit hours of three courses they have taken this semester.  The program should calculate the GPA using following formula.
 The program should then write GPA information into “stdInfo.txt” using the following format.
Text Box: ID: xxxxxxxx GPA: x.x
ID: xxxxxxxx GPA: x.x
ID: xxxxxxxx GPA: x.x
……….

[12]




7.

Bonus: (Total may not exceed 60)



Three class test marks of 30 students are stored in “marks.txt” with the following format.

(format of the file marks.txt)
Text Box: Name: Rashid  CT1= 9 CT2=7  CT3=5 
Name: Hasan  CT1= 10 CT2=4  CT3=10
Name: Sumon  CT1= 8 CT2=7  CT3=3
…………

Write a program to read the information from “data1.txt”. The program should then calculate and show the average class test with the following format.

(format of the output)
Text Box: Name: Rashid  CT average: 7
Name: Hasan  CT average: 8 
Name: Sumon  CT average: 6
…………



UNITED INTERNATIONAL UNIVERSITY (UIU)

Dept. Of Electrical & Electronic Engineering (EEE)

Exam: Final, Trimester: Spring, Year: 2010

Course: CSI 121 (Sec – A,B,C), Title: Structured Programming Language
Marks: 60, Time: 2:00 Hour

There are 6 questions. Answer any 5. Figures in the right-hand margin indicate full marks. 
 


1.

Write the outputs of the following program.

#include <stdio.h>
void main()
{
   char names[][10]={"aaa","bbb","ccc","ddd","eee","fff","ggg","hhh"};

   FILE *fp=fopen("data.txt","w");
   fwrite(names,10*sizeof(char),8,fp);
   fclose(fp);

   char names2[5][10];
   fp=fopen("data.txt","r");
   fread(names2,10*sizeof(char),5,fp);
   for(int i=0; i<5; i++)
               printf("%s \n",names2[i]);

   char name[10];
   fseek(fp,2*10*sizeof(char),SEEK_SET);
   fread(name,10*sizeof(char),1,fp);
   printf("\n %s \n",name);

   fseek(fp,3*10*sizeof(char),SEEK_CUR);
   fread(name,10*sizeof(char),1,fp);
   printf("\n %s \n",name);

   fseek(fp,-1*10*sizeof(char),SEEK_END);
   fread(name,10*sizeof(char),1,fp);
   printf("\n %s \n",name);
  
   fclose(fp);
}
[12]




2.

Write a C program that reads a line of text from user and then counts and prints the words.
[12]








3.

Write the outputs of the following program. Mention “Garbage-value” if any value appearing in the output is undefined.

#include <stdio.h>

union student {
            int ID;
            char grade;
};
enum sems { spring=1, summer, fall};
void main()
{
            student s;
            s.ID=123;
            s.grade='A';
            printf("%d \n",s.ID);
            printf("%c \n",s.grade);

            s.grade='A';
            s.ID=123;       
            printf("%d \n",s.ID);
            printf("%c \n",s.grade);          

            enum sems sm=summer;
            switch(sm)
            {
                        case 1:
                                    printf("Fall\n");break;
                        case 2:
                                    printf("spring\n");break;
                        case 3:
                                    printf("summer\n");break;
            }
}
[12]




4.



Text Box: ID: xxxxxxxx Total marks: xxx.xx
ID: xxxxxxxx Total marks: xxx.xx
ID: xxxxxxxx Total marks: xxx.xx
……….
Write a program to get information of 10 students from user and show their total marks (=mid1+mid2+final) as shown above. Use the following structure to store all the information.
Text Box: struct student {
 int ID; 
 float mid1;
 float mid2;
 float final; 
};
[12]




5.

Write a program to read 50 employers’ information from data1.txt. The program should calculate the weekly salary (=hourly rate * hours) and write it back to data2.txt file.

data1.txt (format of the input file)
Text Box: Name: Rashid  HourlyRate: 20.00 Hours: 40 
Name: Hasan  HourlyRate: 24.50 Hours: 40
Name: Sumon  HourlyRate: 22.00 Hours: 42
…………
data2.txt (format of the output file)
Text Box: Name: Rashid  Salary: 800.00
Name: Hasan  Salary: 980.00 
Name: Sumon  Salary: 924.00
…………
[12]




6.

Write the outputs of the program below:

#include <stdio.h>
#include <string.h>
void main()
{
            int i, j;
            char str[8][10] = { "abc", "xyz", "def", "uvw", "ghi", "rst", "jkl", "opq" };
            char tmp[10];

            for( i=0; i<7; i++ ) {
                        for( j=i+1; j<8; j++ ) {
                                    if( strcmp(str[i],str[j])<0 ) {
                                                strcpy(tmp,str[i]);
                                                strcpy(str[i],str[j]);
                                                strcpy(str[j],tmp);
                                    }
                        }
            }
            for( i=0; i<8; i++ )
                        printf( "%s\n", str[i] );
}
[12]

 

UNITED INTERNATIONAL UNIVERSITY (UIU)
Dept. Of Electrical & Electronic Engineering (EEE)

Exam: Final, Trimester: Summer, Year: 2009

Course: CSI 121 (Sec – A,B), Title: Structured Programming Language
Marks: 60, Time: 2:00 Hour

There are 5 questions. Answer any 4. Figures in the right-hand margin indicate full marks. 
 


1.
a.
Compare the functionality of getch(), getche() and getchar()
[6]

b.
What is the function of fprintf()?
What is the output of the code below?  Where does the output go – screen/file?
             fprintf( stdout, “Hello World” );
[4]

c.
Write C code to print every 4th character of the string below:
    char str[80] = “This is a test to see if you understand string”;
[5]




2.
a.
Where does a pointer point to?
[2]

b.
What are null pointer and dangling pointer?  What are the problems with dangling pointer?
[3+2]

c.
What is the output of the code below:
void main( void ) {
int *ptr, i;
int *x = (int *)malloc( 10 * sizeof(int) );

for( i=0; i<10; i++ )
       x[i] = i+1;
for( i=0; i<10; i++ )
       printf( “ %d”, *(x+i) );

ptr = x;
x[0] += *ptr;
ptr += 3;
*ptr /= *(x + 1);

for( i=0; i<10; i++ )
       printf( “ %d”, x[i] );
free( x );
}

[8]


3.
a.
What are the differences between struct and union?
[3]

b.
How much memory is allocated for mystruct and myunion below:
struct mystruct {                                       union myunion {
           int a;                                                         int a;
           char ch;                                                    char ch;
};                                                               };

[2+2]

c.
Declare a pointer of type mystruct and store an int and a char into its members
[4]

d.
What is the output of the code below:
void main(void)
{
     struct {
         int a;
         char b;
     } x, y;

     x.a = 10;
     x.b = ‘p’;

     y = x;
     printf( “%d%c\n”, y.a, y.b );
}

[4]




4.
a.
What is the output of the code below:
enum semesters { Spring, Summer, Fall } this_semester;
this_semester = Summer;
char names[][15] = { "Spring", "Summer", "Fall" };
printf( "This is %s semester\n", names[this_semester] );

If we declared the enumeration as enum semesters { Spring=1, Summer, Fall }; then what would be the output of the above code?
[4+3]

b.
Write a complete C code to read a text file named letter.txt and show its content on the screen.
[8]




5.

Describe briefly (i.e. no more than 3 sentences) what each of them does?
a) int *x;
b) int *x = (int *)malloc( 20 * sizeof(int) );
c) char *ch = str;                     // str was declared as char str[80];
d) FILE* fp = fopen( “my_file”, “r+” );
e) for( int i=0; str[i]; i++ )      // str was declared as char str[ ] = “Hello World”;
[3*5]


6. Bonus Question (Total may not exceed 60 points)

a.
Create a structure named address that has two members: name and street.  Both name and street are strings.
[3]

b.
Create a structure named student_info containing 3 members: student_address, ID and cgpa.  student_address is of type address defined in (a) above.  ID is an int and cgpa is a float type variable.
[3]

c.
Declare an array of student_info for all 30 students in CSI 121 class.
[1]

d.
Now write C code to print information of all CSI 121 students in the format below.  No need to write complete code, write only the portion needed for printing.  Assume that all students info are stored in the array you declared in (c) above.
<ID>
<Name>
CGPA: <CGPA>
Address: <street address>

For Example:
001001001
Suhas Pai
CGPA: 4.00
Address: 8/A Satmasjid Road
[8]




[12]

No comments:

Post a Comment