Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSCE 150A Lab Assignment 5 - Problem Solving Using Computers, Assignments of Computer Science

Information about lab assignment 5 for csce 150a - problem solving using computers course, which was offered in spring 2009. The assignment includes four problem-solving exercises related to loops. Students are required to write the value of the variable after the loop has completed for each problem.

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-njm
koofers-user-njm 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSCE 150A - Problem Solving Using Computers Laboratory - Spring 2009
Lab Assignment 5 - 02/12/2009
Name:
Section: 8:00 11:00 12:30 6:30
Problem 1: Practice with Loops
Below are four code segments, including a variable initialization and a loop. Write the value that the
requested variable contains after the loop has completed.
int n, m;
n = 1;
for(m=1;m<=5;m++)
{
n = n*m;
}
What is n?
double x, y;
x = 64.0L;
do
{
y = sqrt(x);
} while ( y > 10 );
What is y?
int a, b;
a = 5;
while ( a < 100 )
{
b = a+2;
a=b*a;
}
What is b?
inte=1,f=1,g;
for(g=0;g<10;g++)
{
int temp = f;
e = f;
f = temp + e;
}
What is f?
Problem 2: Programming Loops
Download loops.zip from the lecture outline. Expand it into your Z: drive and open loops.c. There
are blanks in the implementations of the functions squares and exponents where you need to insert a loop
structure.
ONLY WRITE CODE WHERE INSTRUCTED BY THE COMMENTS.
squares takes an integer nand outputs all the squares 12,22,32, . . . , n2. The output code is written,
but the loop is missing. Hint: Use a for loop and calculate the square of the conditional variable.
exponents takes a decimal band an integer mand outputs all powers of bfrom 0 to m:b0,b1, b2, . . . , bm.
The output code is written, but the loop is missing. Hint: keep a counter of the current power, and
use the pow function.
To check your work, type the command testLoops. This will run your program with the input 20 2.5
5. Show the instructor the output. (Hint: if testLoops outputs nothing, then you did it right!)
Bonus: Write the exponents function without using the pow function. Show the instructor your code if
you have done the bonus.

Partial preview of the text

Download CSCE 150A Lab Assignment 5 - Problem Solving Using Computers and more Assignments Computer Science in PDF only on Docsity!

CSCE 150A - Problem Solving Using Computers Laboratory - Spring 2009

Lab Assignment 5 - 02/12/

Name:

Section: 8:00 11:00 12:30 6:

Problem 1: Practice with Loops

Below are four code segments, including a variable initialization and a loop. Write the value that the requested variable contains after the loop has completed.

int n, m; n = 1; for ( m = 1; m <= 5; m++ ) { n = n*m; }

What is n?

double x, y; x = 64.0L; do { y = sqrt(x); } while ( y > 10 );

What is y?

int a, b; a = 5; while ( a < 100 ) { b = a+2; a = b * a; }

What is b?

int e = 1, f = 1, g; for ( g = 0; g < 10; g++ ) { int temp = f; e = f; f = temp + e; }

What is f?

Problem 2: Programming Loops

Download loops.zip from the lecture outline. Expand it into your Z: drive and open loops.c. There are blanks in the implementations of the functions squares and exponents where you need to insert a loop structure.

ONLY WRITE CODE WHERE INSTRUCTED BY THE COMMENTS.

  • squares takes an integer n and outputs all the squares 1^2 , 22 , 32 ,... , n^2. The output code is written, but the loop is missing. Hint: Use a for loop and calculate the square of the conditional variable.
  • exponents takes a decimal b and an integer m and outputs all powers of b from 0 to m: b^0 , b^1 , b^2 ,... , bm. The output code is written, but the loop is missing. Hint: keep a counter of the current power, and use the pow function.

To check your work, type the command testLoops. This will run your program with the input 20 2.

  1. Show the instructor the output. (Hint: if testLoops outputs nothing, then you did it right!)

Bonus: Write the exponents function without using the pow function. Show the instructor your code if you have done the bonus.