You are on page 1of 15

GUIDED LAB 2

CS1010E

Group 31

Observations from Assessed Lab 2

Not done well.

Wrong output format, marked as incorrect!!

Extra ,
Missing ()
Extra \n, scanf(%d\n, &n);

Assessed Lab Qn1

Level 1, print starting


value, using

Input r, output s
Just a for loop

Level 2, all starting


values

Another for loop

Assessed Lab Qn1

Level 3

Level 4, accumulation
of all additional
values

s = s + k;
printf("(%d)", s);

For loop
for (k = row+1; k <= n;
k++)
{
print((%d), k);
}

Level 5

printf(,%d", s);

Guide to assessed lab question 2


int n, max = 0, max2 = 0;
scanf("%d", &n);
while (n > 0) {

scanf("%d", &n);
}
printf("%d %d\n", max2,
max);

if (n > max)
{
max2 = max;
max = n;
}
else
if (n > max2)
{
max2 = n;
}

This weeks theme-- Array

Be aware of

Size of array

Important

Array as input to function

General structure for coding with


functions
#include<>
//function prototype declaration
//func to read
//func to compute
//func to print

int main (void)


{
function calls;
return 0;
}

Modular coding

Tasks of the week

Palindrome number
Big number addition
Moving average (similar to max2 ?)

Steps to solve a task

Read the questions carefully

Think about how you would tackle the problem if it is


not programming

Inputs
Outputs
Relationship between inputs and outputs

Get the algorithm right first!

Implement the algorithm using programming methods

Codecrunch Question
1. Palindromic large number:
Check whether a large number (<= 40 digits) is the
same whether read from left to right or right to left.
12345678987654321 is palindromic
12344321 is palindromic
1231 is not palindromic.

Codecrunch Question
1. Palindromic large number:
Solve the problem using 3 functions:
void readNum(int [], int *);

bool isPalindrome(int [], int);

Read the large number into an array


Check whether it is palindronmic
Compare 1st with last, 2nd with 2nd last . until the middle
one/or not equal

void printNum(int [], int);

Codecrunch Question
2. Add Two Large Number
Add two big number (each <= 40 digits)
Cannot store into integer/add directly Overflow
Solve by 3 functions:
void readNumber(int []);
void add(int [], int [], int []);
void printNum(int []);

Codecrunch Question
2. Add Two Large
Number
555+28=?

Read

1555
+
28
1583

Compute

Read in as
{1, 5, 5, 5, undef, undef}
Transform to
{0, 0, 0, , 0, 1, 5, 5, 5}
Similarly for 28, after
transformation
{0, 0, 0, , 0, 0, 0, 2, 8}
Add from right to left, with carry
{0, 0, 0, , 0, 1, 5, 8, 3}

Codecrunch Question
3. Moving Average
The n-moving average of 10 number sequence {4,9,7,5,-8,-3,0,8,-9,2}
Solve by 3 functions:
void computePrintSeq(int x[], int n);

How many numbers to compute & print


What are the numbers

Practice makes perfect


You would never know what kind of errors you will make
during exams/assessed labs if you do not practice
enough.

You might also like