Docsity

PPT for com sci component 1, Study notes of Computer Science

Toggle sidebar
bg1
Arrays of up to three dimensions, records, lists and tuples
Data structures
1. A business is tracking its annual profit/loss over its first five years.
In a programming language of your choice, write and test the following program, described in
pseudocode.
1. Define a one-dimensional array called
Sales
with five elements.
These elements represent the five years.
2. Loop forever:
An infinite loop can be achieved using the command While True in Visual Basic or while True in
Python.
2.1 Ask the user for a year.
The year is a number – for example, 0 is the first year, 1 is the second year, etc.
2.2 If the element is zero:
The number is the amount of profit (positive) or loss (negative).
2.2.1 Ask the user to enter a number.
2.2.2 Store the number in the array.
2.3 Else:
2.3.1 Output the number already stored.
Abstract view of an
array
Sales
Elemen
t
Data
0 11568
1 32986
2 87876
3
4
pf3
pf4
pf5

Partial preview of the text

Download PPT for com sci component 1 and more Study notes Computer Science in PDF only on Docsity!

Arrays of up to three dimensions, records, lists and tuples

  1. A business is tracking its annual profit/loss over its first five years. In a programming language of your choice, write and test the following program, described in pseudocode.

1. Define a one-dimensional array called Sales with five elements.

These elements represent the five years.

  1. Loop forever: An infinite loop can be achieved using the command While True in Visual Basic or while True in Python. 2.1 Ask the user for a year. The year is a number – for example, 0 is the first year, 1 is the second year, etc. 2.2 If the element is zero: The number is the amount of profit (positive) or loss (negative). 2.2.1 Ask the user to enter a number. 2.2.2 Store the number in the array. 2.3 Else: 2.3.1 Output the number already stored. Abstract view of an array Sales Elemen t Data 0 11568 1 32986 2 87876 3 4

Arrays of up to three dimensions, records, lists and tuples

  1. Modify your program so it can store and retrieve sales figures for each year by quarter – e.g., Jan- Mar, Apr-Jun, Jul-Sep, Oct-Dec. Define a two-dimensional array for this purpose.

Sales [5,4]

Abstract view of an array Sales Quarter 0 1 2 3 Yea r

Yea

r

Quarte

r

Arrays of up to three dimensions, records, lists and tuples

  1. The program is not structured and it’s not very robust. Try these extensions: a) The program should be structured into functions and procedures with comments. b) The input should be validated and independent of the subroutines storing and retrieving the data. c) The program should not loop indefinitely, but instead present a menu option to the user: input year, output year, output all years and end. d) The program should read and write data to a file with these options available from the menu. e) The program should allow the user to name the departments.

Arrays of up to three dimensions, records, lists and tuples

  1. Compare and contrast constants and tuples.
  2. Compare and contrast record structures and classes.