Friday, 31 January 2014

CSC 202 PAST QUESTION FOR 2011/2012 SOLVED IN PYTHON PROGRAMMING LANGUAGE

QUESTION 1B

Write a program that computes the numbers of accumulated AIDS cases A(t) in the Nigeria in year t according to the formula
                A(t) = 174.6(t – 1981.2)

SOLUTION IN PYTHON LANGUAGE

>>> t = input("enter year t :")
>>> A = 174.6 * (( t - 1981.2)**3)
>>> print A


QUESTION 1C

Write a program to compute compound interest on an initial balance over a number of years. Run it for a period of about 10yrs and see if you can follow how it works

SOLUTION IN PYTHON LANGUAGE

>>> t = input("enter years :")
>>> r = input("enter rate: ")
>>> p = input("enter initial balance amount :")
>>> for i in range(1 ,t+1):
     A = p * ((1 + r )**t)
     p = A
>>> print A


QUESTION 2A

Suppose that the final course mark of students attending a university course is calculated as follows. Two examination papers are written at the end of the course. The final mark is either the average of the two papers or the average of the two papers and the class record mark (all weighted equally), whichever is the higher. Write a program to compute and prints each students mark, with the comment PASS or FAIL (50% being the pass mark).

SOLUTION IN PYTHON LANGUAGE

>>> scoreOne = input("enter score for paper one")
>>> scoreTwo = input("enter score for paper two: ")
>>> classScore = input("enter class record mark: ")
>>> averageScore = (scoreOne + scoreTwo) / 2
>>> if (classScore > averageScore):
     if ( classScore >= 50):
           print "PASS"
     else:
           print "FAIL"
elif(averageScore > classScore):
     if(averageScore >=50):
           print "PASS"
     else:
           print "FAIL"
elif (averageScore == classScore):
     if(averageScore >=50):
           print "PASS"
     else:
           print "FAIL"


QUESTION 2B

Write a fortran 90 program to calculate an hourly employee’s weekly pay

SOLUTION IN PYTHON LANGUAGE

>>> hoursWorked = input("how many hours worked :")
>>> hoursWorked_perDay = input("how many hours worked per day: ")
>>> nosOfDays_perWeek = input("enter nos of days worked per week :")
>>> weeklyPay = input("enter amount pay weekly :")
>>> total_hours = hoursWorked_perDay * nosOfDays_perWeek
>>> hourlyPay = weeklyPay / total_hours
>>> print hourlyPay


QUESTION 2C

Write a fortran 90 program to calculate the hypotenuse of a triangle from the two sides

SOLUTION IN PYTHON LANGUAGE

import math
>>> sideA = input("input side A for the triangle :")
>>> sideB = input("input side B for the triangle :")
>>> sideC = math.sqrt ((sideA**2) + (sideB)**2)
>>> print sideC


QUESTION 3A

A class of students write a test and each student name (max of 15characters) and mark is entered in a data file. Assume there are no negative marks. Write a program which prints out the name of the students with the highest mark, together with his or her mark. Assuming that there is only one highest mark. A first level structured plan could be
-          Start
-          Find top student and top mark
-          Print top student and top mark
Step 2 needs elaborating, so a more detail plan might be:
-          Start
-          Initialize top mark(to get process going)
-          Repeat for all students
-          Read name and mark
-          If mark > topmark then
Replace topMark with mark
Replace topName with name
-          Print topName and topMark
-          Stop

SOLUTION IN PYTHON LANGUAGE

>>> datafile = open("myFile2.txt","r")
>>> text = datafile.readline() 
>>> listOfScore = []
>>> for  s  in  text:
     score = s.split(" ")
     listOfScore.append ( score[0])

>>> highest = listOfScore[0]
>>> for k in range(1,len(listOfScore)):
     if( highest < listOfScore[k] ):
           highest = listOfScore[k]
           index = k
>>> print text[index]


QUESTION 3C

Write a fortran program which computes n!

SOLUTION IN PYTHON LANGUAGE

>>> n = input("enter n value to be computed")
>>> i = 1
>>> for  k  in  range(1,n+1):
      i *= k   

>>> print i

PYTHON QUESTION 1 FROM: MOTUNRAYO AFOLABI

Assume" s" is a string of lower case characters. Write a program that count up the number of vowels contained in the string "s". Valid vowel are: "a", "e", "i", "o", and "u". For example, if s="azcbobobegghakl", your program should print:
Number of vowels :5

SOLUTION

textList = []

text = raw_input("Enter any string")

textList = list(text)              #The function of keyword "list" is to turn test to list of words

a = textList.Count("a")       #The "Count" function helps to count the number of letters

e = textList.Count("e")

i = textList.Count("i")

o = textList.Count("o")

u = textList.Count("u")

vowelCount = a + e + i + o + u

Print(vowelCount)

PYTHON QUESTION 2 FROM: MOTUNRAYO AFOLABI

Assume that there are n tyres that could be used by car, tricyle and bicyle. Write a program to accept n tyres and generate all the combinations (i.e the number) of car(s), tricycle(s) and bicyle(s) that could exhaust the n tyres. Ensure that all the objects have fair share where possible

SOLUTION

>>> car = 4
>>> tricycle = 3
>>> bicycle = 2
>>> def lessThanNineTyres(n,c,t,b):
     if n == 4:
           c += 1
     elif n == 3:
           t += 1
     elif n == 2:
           b +=1
     else:
           if (n == ( car + tricycle)):
                c += 1
                t += 1
           elif (n == (car + bicycle)):
                c += 1
                b += 1
           elif (n == (tricycle + bicycle)):
                t += 1
                b += 1
           else:
                if (b > t):
                     t += 2
                     b += 1
                else:
                     b += 2
                     c += 1
     countList = [c,t,b]
     return countList

>>> c = 0
>>> t = 0
>>> b = 0
>>> allTogether = 9
>>> n = input("enter numbers of tyres :")
 if n >= 9 :
     a = n / allTogether
     t += a
     c += a
     b += a
     k = n % allTogether
     if k > 1:
           countList =[]
           countList = lessThanNineTyres(k,c,t,b)
           c += countList[0]
           t += countList[1]
           b += countList[2]
else:
     result = lessThanNineTyres(n,c,t,b)
     c += result[0]
     t += result[1]
     b += result[2]
    

>>> print "cars :",c,"\t tricycle :",t,"\t bicycle :",b

PYTHON TUTORIAL QUESTION 1 FROM: YEMISI TOYIN IDOWU

A man spent half of .his monthly income on foodstuffs.,one third on transport and he also save 1/4 of the ammount left, leaving 75 in his pocket. write a python program to calculate his monthly income?

SOLUTION

fd = 0.5

tp = 0.33

s = 0.042

sum = fd + tp + s

pf = 1 - sum

Income = 75 / pf

Print Income

Thursday, 23 January 2014

CSC 102 CURRENT PAST QUESTIONS AND ANSWERS (contd)


1a.        Write a program to accept a positive integer and then prints out all the positive divisors of that integer in a column and in decreasing order. The program should allow the user to repeat this process as many times as the user likes.
                ANSWER:
                DO
           DO
               INPUT “ENTER A POSITIVE INTEGER”, POST
           LOOP WHILE (POST < 1)
           FOR J = POST TO 1 STEP – 1
           IF (POST MOD J) = 0 THEN
                PRINT J
           ENDIF
     NEXT J
     INPUT “DO YOU WANT TO COMPUTE ANOTHER (Y/N)”; RE$
     LOOP WHILE (RE$ = “y” OR RE$ = “Y”)
     END

b.         Write a program to generate odd numbers divisible by 5, its square and product of squares below 90 between 1 and 101
                ANSWER:
                                LET PRD = 1
           PRINT ”ODD NUMBER, SQUARE, PRODUCT OF SQUARE”
           FOR J = 5 TO 101 STEP 5
           IF (J MOD 2) <> 0 THEN
                PRINT J, J^2
                PRD = PRD * J^2
                IF PRD < 90 THEN
                     PRINT PRD
                ENDIF
           ENDIF
           PRINT
           NEXT J
          END
2a        A positive integer n is said to be prime (or, “a prime”) if and only if n is greater than 1 and is divisible only by 1 and n. for example, the integers 17 and 29 are prime, but 1 and 38 are not prime. Write a function that takes a positive integer argument and returns as its value the integer 1 if the argument is prime and return the integer 0 otherwise.
                ANSWER:
                DO
           INPUT “A POSITIVE INTEGER NUMBER”; N
     LOOP WHILE (N<0)
     IF N > 1 THEN
           PRINT “POSITIVE INTEGER STATUS = ”; SUM(N)
     ELSE
           PRINT “1 IS NOT A PRIME NUMBER”
     ENDIF
     FUNCTION SUM (N)
     LET M = 0
     LET J = INT (N^0.5)
     FOR I = 1 TO J
           IF (N MOD I = 0) THEN
                M = M + 1
           ENDIF
     IF M > 0 THEN
           EXIT FOR
     ENDIF
     NEXT I
     IF M >= 1 THEN
           SUM = 0
     ELSE
           SUM = 1
     ENDIF
     END FUNCTION
B.        Write a program to add all the natural numbers below one thousand that are multiples of 3 or 5
                ANSWER:
                LET SUM = 0
     FOR J = 1 TO 999
           IF (J MOD 3 = 0) OR (J MOD 5 = 0) THEN
                SUM = SUM + J
           ENDIF
     NEXT J
     PRINT “SUM OF NATURAL NUMBERS THAT ARE MULTIPLES OF 3 OR 5”; SUM
     END
3      The table below captures a weekly sales data for a small retail shop
                Product/day                         1              2            3              4             5             6
                Milk                                     20           45           20           40           50           35
                Sugar                                   30           10           90           45           15           60
                Notebook                             10           20           80           12           15           50
                File                                       34           24           50           16           25           50
                Pencil                                   56           43           12           15           20           35
                Biro                                      23           59           20           10           40           30
                Develop a program to determine the following:
(i)                 Total sales for the week
(ii)               The product with highest total sales in the week
(iii)             The total amount of sales for two products (e.g biro and pencil)

ANSWER:
DIM PROD$(6), EPROD(6)
INPUT SECTION
LET SUM = 0
FOR PROD = 1 TO 6
     LET TOTAL = 0
     INPUT “ENTER PRODUCT = ”; PROD$(PROD)
     READ PROD$(PROD)
     DATA “MILK”, “SUGAR”, ”NOTEBOOK”, “FILE”, “PENCIL”, “BIRO”
FOR D = 1 TO 6
     INPUT “ENTER PRODUCT SALES”; AMT
READ AMT
LET TOTAL = TOTAL + AMT
NEXT D
EPROD(PROD) = TOTAL ‘COMPUTE TOTAL SLAES FOR EACH PRODUCT IN THHE WEEK’
SUM = SUM + EPROD(PROD) “TOTAL SALES  FOR THE WEEK”
NEXT PROD
DATA 20, 45, 20, 40, 50, 35
     DATA 30, 10, 90, 45, 15, 60
     DATA 10, 20, 80, 12, 15, 50
     DATA 34, 24, 50, 16, 25, 50
     DATA 56, 43, 12, 15, 20, 35
     DATA 23, 59, 20, 10, 40, 30
‘COMPUTE PRODUCT WITH HIGHEST TOTAL SALES’
LET HSUM = 0
LET PSUM = 0
FOR D = 1 TO 6
     IF (HSUM < EPROD(D)) THEN
           HSUM = EPROD(D)
           PSUM = D
     ENDIF
NEXT D
PRINT “TOTAL SALES FOR THAT WEEK = ”; SUM

‘COMPUTE TOTAL AMOUNT OF SLAES FOR TWO PRODUCTS’
FOR J = 1 TO 5
     FOR H = J + 1 TO 6
           PRINT “SALES OF”; PROD$(J); “AND”; PROD$(H); “=”; EPROD(J) + EPROD(H)
     NEXT H
NEXT J
END

Wednesday, 22 January 2014

CSC 102 CURRENT PAST QUESTIONS AND ANSWERS


1.       What is Microcomputer? How do microcomputers differ from mainframe computers?
ANSWER: A microcomputer is simply a system based on microprocessor usually small and inexpensive.
Microcomputer is a single user system while mainframe is a multiuser system, Microcomputer is portable and inexpensive, Mainframe uses magnetic tapes and disks as data storage, Mainframe requires special environment to work.
2.       Differentiate low level language from high level language?
ANSWER: low level languages (assembly and machine language) are machines dependent but execute faster than high level language because the latter is the actual language of the system while former has a one to one relationship
High level languages are problem oriented and written using English-like expressions and arithmetic operations
3.       Write LET statements that correspond to each of the following algebraic equations
R = (P + q) ½
                P = Ai (1 + i)n/[(1 + i)n - 1]
                ANSWER:
LET R = (P + q)^0.5
                LET P = Ai * (1 + i)^n/((1 + i)^n - 1]
4.       What is a logical error? Differentiate logical error from syntax error?
ANSWER: A logical error occurs when the programmer supplied instruction that are logically incorrect and such error may be difficult to detect.
Syntax error occurs as a result of misspelled keywords, reference to undefined variables, unbalance parentheses, incorrect punctuations, among others which will prevent the program from compiling successfully, while logical error will not halt compilation process but result in incorrect results.
5.       Point out the errors in this program. Correct this and determine the output
LET M = 6
IPUT “Enter a positive integer number” N --- ANSWER: N is missing in IPUT and a semicolon
FROM J = 1 TO M (FOR instead of FROM)
LET N = N + J; --- ANSWER: semicolon not required
PRN “VALUE OF N” WHEN J = “, J” IS ”: N ---- ANSWER: PRINT is the correct keyword not PRN and ‘’ is not required before WHEN.
(Missing next statement)
END
                CORRECT PROGRAM CODE
                                LET M = 6
                                INPUT “ENTER A POSITIVE INTEGER NUMBER” ;N
                                FOR J  = 1 TO M
                                LET N =  N + J
                                PRINT ‘’VALUE OF N WHEN J = “, J; “IS”; N
                                NEXT J
                                END
                SOLUTION
                ENTER A POSITIVE INTEGER NUMBER 5 (THE OUTPUT DEPENDS ON THE INPUT VALUE OF N)
                VALUE OF N WHEN J = 1 IS 6
                VALUE OF N WHEN J = 1 IS 8
                VALUE OF N WHEN J = 1 IS 11
VALUE OF N WHEN J = 1 IS 15
VALUE OF N WHEN J = 1 IS 20
VALUE OF N WHEN J = 1 IS 26
6.       Write an appropriate IF-THEN statement or an IF-THEN-ELSE block for each of the following situations.
Test the value of variable total, if total is less than or equal to 100, add the value of the variable to total and display the value. If total exceeds 100, then display its value, add its value so that it equals 100 and then display the new value.
                ANSWER:
                IF (total <=100) THEN
                                Total v + total
                                PRINT total
                ELSE
                                PRINT total
                LET total = 100  
                PRINT total
                ENDIF
Suppose the variable pay has been assigned a value of 450. Test the value of the variable hours. If hours exceed 40, assign the value 625 to pay
ANSWER:
                LET PAY = 450
                IF (hours > 40) THEN
                                PAY = 625
                ENDIF
7.       State the role of an operating system
ANSWER: Memory management, Processor management, I/O management and file management.
8.       List any five visual basic controls contained on the toolbox
ANSWER: text box, label, command button, list box, image, picture, frame
9.       Explain the term algorithm and itemize the steps involved in programming
ANSWER: Algorithm is a finite set of well-defined rules for the solution of a problem in a finite numbers of steps.
Steps:
Problem definition
Devising the solution method
Developing the method using suitable aids
Writing the instructions in a programming language.
Debugging the program
Testing the program
10.   Define computer software and explain two major groups of software
ANSWER: Software are programs, procedures, rules and any associated documentation pertaining to the operation of a computer system

Application Software – designed to be put to specific practical use. Examples of application packages include excel, word processing and specialize application like payroll system

System Software – controls the way the computer operates or provides facility which extends the general capabilities of the system. Examples are operating system, translators, utilities and service programs.
11.   Write DOS command equivalent to the following
ANSWER:
Copy all files with filename extension BAS from the root directory to CSC 202 subdirectory
                C:\>COPY *.BAS\CSC 202
Delete all files whose names begins with dot regardless of the filename extension
C:\>del dot.*
12.   With the aid of an example, explain the term system configuration.
ANSWER:
System configuration can be referred to as specification of a typical system, which include memory capacity – RAM and hard disk, processor type and speed, operating system among others e.g Intel core i7 200Ghz, 6GB RAM, 640GB HDD, windows 7 O/S