Documente Academic
Documente Profesional
Documente Cultură
MATRICULATION NO. IDENTITY CARD NO. TELEPHONE NO. E-MAIL LEARNING CENTRE
: : : : :
NO TITLE 1 Question 1 1.0 Problem 1.1 Input 1.2 Calculation 1.3 Output 1.4 Programming (coding) 1.5 Answer 2 Question 2 2.0 Problem 2.1 Input 2.2 Output 2.3 Programming (coding) 2.4 Answer
<DATA STRUCTURE><CBDS2103> QUESTION 1 1.0 PROBLEM Required to calculate the average and total of sales for each a year, each of staff and also for a quarter. 1.1 INPUT Average of the staff, quarter and a year. Total of a year, staff and quarter. The overall average of sales and total of sales for a year. CALCULATION Total Quarter (total=total+sales) Average Quarter (avgtotal=total/6) Total sales a year(sum=sum+sales) Average sales a year (yearavg=sum/4) 1.3 OUTPUT The total of sales of the year. The average of sales for the year.
1.2
<DATA STRUCTURE><CBDS2103>
1.4
PROGRAMMING (CODING) #include <string.h> #include <conio.h> void main() { int i, j, sum=0,total2,total,total1; float yearavg=0, avgtotal2=0, avgtotal=0; int sales [6][5]= {{100802, 5500, 7000, 5500, 6800}, {100888, 4800, 6700, 7000, 6500}, {100188, 6890, 2000, 3500, 2500}, {100189, 4000, 5000, 5500, 6000}, {100800, 1210, 4000, 8000, 8000}, {100122, 8000, 12000, 14000, 10000}};
printf("****************************************************************** **************\n"); printf(" * * * * * * * \n"); printf("StaffID * Quarter1 * Quarter2 * Quarter3 * Quarter4 * Total * Average * \n"); printf(" * * * * * * * \n"); printf("****************************************************************** **************\n"); for(i=0;i<6;++i) { printf("\n %d",sales[i][0]);
<DATA STRUCTURE><CBDS2103> }
%d",i,total2);
<DATA STRUCTURE><CBDS2103> yearavg= (float)sum/4; } } printf("\n\n\n *** THE TOTAL SALES OF THE YEAR IS *** : %d", sum); printf("\n\n *** THE AVERAGE SALES FOR THE YEAR IS *** : %0.2f", yearavg); getch(); }
<DATA STRUCTURE><CBDS2103> QUESTION 2 2.0 PROBLEM Consider the problem of recognizing whether a particular string is in the language, and the programs have to determining whether a give string is in L. L={w$w : w is a possibly empty string of characters other than $,w=reverse(w)}.
2.1
INPUT User will enter the string. OUTPUT Result will displayed once entered the string. Example : Output 1: Enter a string : ABC$CBA Result : Inserted string is in the language Output 2: Enter a string : ABCD$CB Result : Inserted string is not in the language
2.2
<DATA STRUCTURE><CBDS2103> 2.3 PROGRAMMING (CODING) #include <stdio.h> #include <stdlib.h> #include <conio.h> #define SIZE 10 typedef struct { int items[SIZE]; int top; }STACK;
void push(STACK *p, int element); int pop(STACK *p); void display(STACK s); int isoverflow(int top); int isempty(int top); int main() { STACK s; char str[100]; int i, frame; s.top = -1; printf("\nEnter a string: "); gets(str); for(i=0;str[i]!='\0'; i++) push(&s, str[i]); frame = 1; for(i=0;str[i]!='$';i++) { if(str[i] != pop(&s)) { frame = 0;
9
<DATA STRUCTURE><CBDS2103> break; } } if(frame == 1) printf("\nRESULT: *** STRING IS IN THE LANGUAGE ***\n"); else printf("\nRESULT: *** STRING IS NOT IN THE LANGUAGE ***\n"); getch(); }
void push(STACK *p, int element) { if(isoverflow(p->top)) { printf("\nStack is overflow"); } else { (p->top)++; p->items[p->top] = element; } } int pop(STACK *p) { if(isempty(p->top)) { printf("\nStack is underflow"); } else { return (p->items[(p->top)--]); } } void display(STACK s) {
10
<DATA STRUCTURE><CBDS2103> int i; if(isempty(s.top)) { printf("\nStack is empty"); } else { for(i=s.top; i>=0; i--) { printf("\n%d", s.items[i]); } } }
int isoverflow(int top) { if(top == SIZE - 1) return (1); else return (0); }
int isempty(int top) { if(top == -1) return (1); else return (0); }
11
ii.
12