0% found this document useful (0 votes)
61 views5 pages

W25 - Lab 06 - Arrays

This document outlines Lab 06 for CPS188, focusing on arrays in C programming. Students are tasked with writing algorithms and C programs to normalize data, sort arrays, and implement stack operations using arrays. The lab includes specific problems to solve, submission guidelines, and a trial run for practice before the exam.

Uploaded by

mahbodkyani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views5 pages

W25 - Lab 06 - Arrays

This document outlines Lab 06 for CPS188, focusing on arrays in C programming. Students are tasked with writing algorithms and C programs to normalize data, sort arrays, and implement stack operations using arrays. The lab includes specific problems to solve, submission guidelines, and a trial run for practice before the exam.

Uploaded by

mahbodkyani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

CPS188 > Lab 06 > Arrays 1

CPS188 : Computer Programming Fundamentals


Lab 06 > Arrays [Winter 2025]

I. Overview and Objectives


Arrays are a kind of data structure that can store a fixed-size sequential collection
of elements of the same type. An array is used to store a collection of data, but it is
often more useful to think of an array as a collection of variables of the same type.
Instead of declaring individual variables, such as number0, number1, ..., and
number99, you declare one array variable such as numbers and use numbers[0],
numbers[1], and ..., numbers[99] to represent individual variables. A specific
element in an array is accessed by an index. All arrays consist of contiguous
memory locations. The lowest address corresponds to the first element and the
highest address to the last element.

The learning objective of this lab is to practice working with arrays of one and two
dimensions and to use them with functions.

Reading and related topics: Course slides Lesson 07. Book chapter 7.

II. Lab Tasks and Submission Guideline

Write algorithms and complete C programs to solve the following two problems.
Follow the template for each problem and make sure your programs are well
documented (comments).

●​ C Programs Lab Report Template

Save your report in .pdf format and submit it on D2L. You should submit your
lab at the end of your lab session or soon after. In all cases it must be submitted
before the deadline indicated in the D2L dropbox or it will not be accepted for
CPS188 > Lab 06 > Arrays 2

marking.

Problem 1: A set of numbers xi in the range xlow to xhigh can be "normalized" into
a new range min to max, by calculating each normxi from each xi as follows:

normxi = min + (xi - xlow) * (max - min) / (xhigh -xlow)


Write a complete C program to read from a data file a number n (an integer
between 3 and 20) and two real numbers (min and max). Then read the set
of n real numbers for xi into an array. Normalize the xi values into the range
min to max, placing the normalized values into a second array. Finally
print the original and normalized values in a two-column table, with
appropriate column headings.
Use the following sets of data to test your program. Put all three
high-resolution screenshots in your report.
7 0.0 10.0 67.9 45.2 33.3 66.1 83.5 14.3 50.5
11 0.0 1.0 6.9 4.2 3.3 6.1 8.5 1.3 5.5 9.9 8.0 3.6 2.8
5 0.0 100.0 -34.3 50.9 0.0 43.2 -77.7

Problem 2: The slides 33 and 34 in Lesson 07 of the generic slides explain


the built-in qsort function found in the C standard library.
You will need to write the sorting functions and the comparison functions
that are necessary to solve the following problem. No need to write the
algorithm for this problem.
A file contains a mix of integer and floating point numbers. Read the
numbers from the file and place them into two different arrays: one array of
integers and one array of doubles. Next, perform the following actions:
CPS188 > Lab 06 > Arrays 3

●​ Sort the array of integers in ascending order and print it.


●​ Sort the array of doubles in ascending order and print it.
●​ Sort the array of integers in descending order and print it.
●​ Sort the array of doubles in descending order and print it.
Note: You must use the qsort function as presented in the slides. You
cannot use any other method to sort the arrays.

Problem 3: Figure 7.13 in the textbook (slide 32 of Lesson 07 of the


generic slides) shows the two functions pop and push that deal with a stack
of characters implemented with arrays. Write the program by completing
the main program that does the following:
1.​ Call the push function five times.
2.​ Print out the updated stack.
3.​ Call the pop function twice
4.​ Print the updated stack again.
No need to write the algorithm for this problem. For your convenience, the
functions are re-typed here as well as the pre-processor directives and the
skeleton of the main program.

#include <stdio.h>
#define STACK_EMPTY '0'
#define STACK_SIZE 20

void
push(char stack[], /* input/output - the stack */
char item, /* input - data being pushed onto the stack */
int *top, /* input/output - pointer to top of stack */
int max_size) /* input - maximum size of stack */
{
if (*top < max_size-1) {
++(*top);
stack[*top] = item;
}
}

char
pop (char stack[], /* input/output - the stack */
CPS188 > Lab 06 > Arrays 4

int *top) /* input/output - pointer to top of stack */


{
char item; /* value popped off the stack */

if (*top >= 0) {
item = stack[*top];
--(*top);
} else {
item = STACK_EMPTY;
}

return (item);
}

int
main (void)
{
char s [STACK_SIZE];
int s_top = -1; // stack is empty

/* complete the program here */

return (0);
}

Test Trial Run:


During the last half hour you will practice the test environment with a trial run
test. The computer will be rebooted into the proprietary online exam system.
Follow your lab TA instructions. You will see the test details from the link below
in the exam environment. The exam environment for C programming is Geany on
Unix (very similar to Geany on Mac or Windows).

Lab 06 : Practice Test

Have fun!
CPS188 > Lab 06 > Arrays 5

You might also like