Sunteți pe pagina 1din 6

q6ciii2010

function to accept character ch and return 1 if character is present in


string CAPECOMPUTERSCIENCE. The function should return 0 if ch is not
present in string

parameter:checkstring(ch);

//correction:character variable is ch....while(str[i]!='\0'{...})


int checkstring(char ch)
{
char str="CAPECOMPUTERSCIENCE";
int i,j;

for(i=0; str[i]; i++);


{

if (str[i] ==ch[0])
{ if (str[]==findstr[1])
return 1;
}
}
return 0;
}

2011
6. a. White space is an unprintable character of a specific width and length
6.b Debugging is the process of revising a code to find and remove/handle
errors found in the program
6.c Programmers must practice proper indentation to improve readability of
the code so it is easier to navigate for themselves and potentially other
programmers who might have to deal with the code later on+ easier to modify
and maintain
6.d
#include <stdio.h>

int main()
{
int a,b,c;

scanf("%d"a);
scanf("%d"b);
scanf("%d"c);

if ( ((a<=0||b<=0)||c<=0) || ((a>b+c||b>a+c)||c>a+b) )
printf("No triangle is represented");
else if (a==b&&b==c)
printf("Equilateral Triangle");
else if ((a==b||b==c)||c==a)
printf("isosceles triangle");
else if ((a!=b&&b!=c)&&c!=a)
printf("scalene triangle");
return 0;
}

////
e.i.
11111
22222
33333
44444

ii.

int i,j;
for (j=1;j<=5;j++)
{
for(i=j;i<=5;i++)
{
print("%d ",i);
}
print("\n");
}

c)
//output
*
$ &
$ &
$ &
$ &
$ &
+++++++++++

//end of output
2012
A.
lexical analysis,syntax analysis, semantic analysis
b.

assumption:n is not zero or a negative number

int answer=findexp(n);

int findexp(int n){


int i,answer=2;
for (i=2;i<=n;i++){
answer=answer*2;
}
return answer;
}

c.i
int main()
{
int i;
FILE *fp;

fp= fopen("num.dat","w");
for (i=2;i<=40;i++){
if (i%2==0)
fprintf(fp,"%d\n",i);
}
fclose(fp);
return 0;
}

ii)
int main(){
int i;
FILE *fp;

fp= fopen("num.dat","r");
while(fscanf(fp,"%d",i)>0){
fscanf(fp,"%d\n",i);
printf("%d",i+5);
}
fclose(fp);
return 0;
}

6.a
Use of white spaces,i.e use of proper indentation as this helps you to
understand the program better and keep it organised
Make use of comments(documentation of the program) so that you as well as
other programmers who may have to read the code will understand exactly
what the purpose of a certain piece of code is and how it contributes to
solving the problem
Meaningful identifiers;Use appropriate names for your variables, user
defined functions and such so the code is easy to understand

b.
struct productRec{
int ID;
int stockq;
float price;
};

ii.
struct productRec item1,item2;
iii.
item1.ID=121;
item1.stockq=44;
item1.price=22.40;
iv.
int idtemp,stockqtemp;
float pricetemp;

idtemp=item3.ID;
stockqtemp=item3.stockq;
pricetemp=item3.price;

item3.ID=item4.ID

//////////////////////////////////////////
struct productRec temp;
temp=item3;
item3=item4;
item4=temp;
c.
int main()
{
char str[8];
int i,acount;
printf("enter name\n");
scanf("%7s",str);
acount=1;
for(i=0;i<7){
if (str[i]==A){
acount+=1;
printf("Vowel A present. ");
}
else if (str[i]==E) printf("Vowel E present. ");
else if (str[i]==I) printf("Vowel I present. ");
else if (str[i]==O) printf("Vowel O present. ");
else if (str[i]==U) printf("Vowel U present. ");
}
printf("\nNumber of As=%d",acount);
return 0;
}
2013
5. a
interpreters convert the program
instructions from high level language code one statement at a time during
execution
whereas compilers translate all high level language program source code
into object code prior to execution(machine code or assembly language).
As a result the executable file can run very quickly.
b.
swap();

void swap(int arr[],int n,int pos1,int pos2){


int tempstore;
tempstore=arr[pos1];
arr[pos1]=[pos2];
arr[pos2]=tempstore;
}
c.
#include <stdio.h>

int main(){
int num1,num2,sum,prod;
FILE *fp;

fp=fopen("in.dat","r")
fscanf(fp,"%d %d",num1,num2);
fclose(fp);

printf("%d %d",num2,num1);
sum=num1+num2;
prod=num1*num2;
printf("\nsum=%d, product=%d",sum,prod);
return 0;
}

d.
#include <stdio.h>
int

2014

5.a.i
lexical analysis
ii. code optimization

b. the role of 'watch' in programming- a debugging tool where


the values of the variable are viewed
c./////////////////doublecheck passing an array//////////////
int less20count=countarr(arrsize,arr[arrsize]);

int countarr(int arrsize,int arr[]){


int i,less20count=0;

for(i=0;i<arrsize;i++){
if (arr[i]<20) less20count+=1;
}
return less20count;
}
d.
#include <stdio.h>
int main(){
float funds,totalfunds=0;
int donationcount=0;
FILE *fp

fp=fopen("donations.txt","r");
fscanf(fp,"%f",&funds);
do{

fscanf(fp,"%f",&funds);
if (funds==-1) break;
totalfunds+=funds;
donationcount+=1;
}while(funds!=-1);
fclose(fp);
fp=fopen("funds.txt","w");
fprintf(fp,"Totalfunds=%.2f",totalfunds);
fprintf(fp,"Donations made=%d",donationcount);
fclose(fp);
return 0;
}
2015
5.a.i
lexical analysis is to "tokenize" the source code/ convert the programs
lexicon to a series of tokens and remove all comments and white spaces
ii.at that point they limit the length of identifiers and string constants
and errors are flagged in order to avoid overflow or underflow
b. modular programming is a software design technique that
encourages breaking up large problems into smaller ones in the form of
successive modules which are independent of each other thus making solving
large problems easier and the sub problems can be worked on as a group
-sharable,readable,easily modified &maintained
c.
int main(){
int mark, highestmark=-1;
int a=0,b=0,c=0,f=0;
char id[6];
FILE *fp;

fp=fopen("testgrades.txt","r");
while (fscanf(fp,"%5s %d",id,&mark)>0){
if (mark>highestmark)
highestmark=mark;

if (mark=>80&&mark<=100)a+=1;
else if (mark=>70&&mark<=79)b+=1;
else if (mark=>60&&mark<69)c+=1;
else if (mark<=59)f+=1;
}
printf("highest mark is %d",highestmark);
printf("Total As= %d",a);
printf("Total Bs= %d",b);
printf("Total Cs= %d",c);
printf("Total F= %d",f);

fclose(fp);
return 0;
}
d.
#include <stdio.h>
#include<string.h>
int main(){
char wrd[50];
int i,strleng,palin=1;

printf("enter word");
gets(wrd);
strleng=strlen(wrd);
for (i=0;i<strleng;i++){
if (wrd[i]!=wrd[strleng-i]){
printf("not a palindrome");
palin=0;
break;
}
}
if (palin==1) printf("word is a palindrome");
return 0;
}

S-ar putea să vă placă și