0% found this document useful (0 votes)
18 views

Worksheet On Arrays Parallel Arrays 2018

Uploaded by

sjurie21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Worksheet On Arrays Parallel Arrays 2018

Uploaded by

sjurie21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

ONT1000 / SDS1000

Worksheet on Arrays (Parallel Arrays)


Name: Surname: Student Number: Subject
Code:

QUESTION 1

QUESTION 1.1

Identify and correct the FIVE errors in this code, to ensure it works as shown.

The program below has two parallel arrays. It prompts for the temperatures for each day
and stores these into the temperatures array.

static void Main(string[] args)


{

string[] days = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",


"Friday"};

int temperatures = new int[8];

int sum = 0, average;

string message1 = "Above average temperatures:\n",


message2 = "Below average temperatures:\n";

for (int i = 0; i <= temperatures.Length; i++)


{
Console.Write("Enter temperature for {0}: ", days[i]);

temperatures = int.Parse(Console.ReadLine());
}

Page 1 of 8 Compiled by Yiota Moutzouris


QUESTION 1.2

Identify and correct the SEVEN errors in this code, to ensure it works as shown.

The program continues and now determines the average.

It then prints all the temperatures above the average and all the temperatures below the
average in a message box, as shown.

for (int i = 0; i < temperatures.Length)


{
sum = sum + temperatures[i];
}

average = sum / temperatures;

for (int i = 1; i < temperatures.Length; i++)


{

if (temperatures[i] > average)


message1 = "Temperature - " + days[i] + ": " +
temperatures[i] + "\n";

else if (temperatures[i] < average)


message2 = "Temperature - " + days[i] + ": " +
temperatures[j];

MessageBox.Show("Average temperature: " + average + "\n\n" +


message1 + message2);

Console.ReadLine();
}

Page 2 of 8 Compiled by Yiota Moutzouris


QUESTION 2

Identify and correct the SEVEN errors in this code, to ensure it works as shown.

The program has five parallel arrays, namely, skaterName, score1, score2, score3 and
score4. It determines the highest per skater and prints this value out as shown.

static void Main(string[] args)


{

string[] skaterName = { "Paula", "Zinzi", "Asha"};

int[] score1 = { 6, 8, 7, 8 };
int[] score2 = { 6, 9, 7, 8 };
int[] score3 = { 4, 10, 8, 5 };
int[] score4 = { 7, 8, 7, 7 };

int highest = 99;

Console.WriteLine("High Scores: ");

for (i = 0; i <= skaterNames.Length; i++)


{

highest = Math.Max(highest, score1[i]);


highest = Math.Max(highest, score2[i]);

highest = Math.Max(highest, score3[i]);


highest = Math.Max(highest, score3[i]);

Console.WriteLine("The highest score for {0} was {2}", skaterName[i],


highest);

Console.ReadLine();
}

Page 3 of 8 Compiled by Yiota Moutzouris


QUESTION 3

This question deals with the petName and petType parallel arrays.

QUESTION 3.1
View the code below and indicate what will be printed to the console window.
string[] petName = new string[4];
string [] petType = new string[4]; // example would be dog, cat, bird, etc.

for (int i = 0; i < petType.Length; i++)


{
Console.Write("Please enter pet type {0}: ", i+1);
petType[i] = Console.ReadLine();
Console.Write("Please enter the name of the pet {0}: ", petType[i]);
petName[i] = Console.ReadLine();
}
Assume user will enter “Zorro” for a dog, then “Lassie” for a bird, then “Speckles” for a cat, then
Spotty for a fish.

QUESTION 3.2
View the code of Question 3.1. Show the contents of the petName and petType parallel arrays.

If necessary, indicate more indexes in the diagram below.

petName
(0) (1)

petType
(0) (1)

QUESTION 3.3
View the code of Question 3.1 and answer the following questions.
3.1 Indicate the final value for the variable i
3.2 Indicate the value for petType.Length

Page 4 of 8 Compiled by Yiota Moutzouris


QUESTION 3.4
View the code below.

What will be printed to the console window?

If an error is likely to occur, explain this error.


string[] petName = new string[4];
string [] petType = new string[4];

for (int i = 0; i <= petType.Length; i++)


{
Console.WriteLine("Values of 4 pets: ");
Console.Write("Please enter pet type {0}: ", i+1);
petType[i] = Console.ReadLine();
Console.Write("Please enter the name of the pet {0}: ", petType[i]);
petName[i] = Console.ReadLine();
}
Assume user will enter “Zorro” for a dog, then “Lassie” for a bird, then “Speckles” for a cat, then
Spotty for a fish.

QUESTION 3.5
View the code below.

What will be printed to the console window?

string[] petName = new string[4];


string [] petType = new string[4];

for (int i = petType.Length - 2; i > 0; i--)


{
Console.WriteLine("Values of 4 pets: ");
Console.Write("Please enter pet type {0}: ", i + 1);
petType[i] = Console.ReadLine();
Console.Write("Please enter the name of the pet {0}: ", petType[i]);
petName[i] = Console.ReadLine();
}

Assume user will enter “Zorro” for a dog, then “Lassie” for a bird, then “Speckles” for a cat, then
Spotty for a fish, if the code segment loops and asks for 4 pets.

Page 5 of 8 Compiled by Yiota Moutzouris


QUESTION 4

The following program deals with names and their ages. Name and age are parallel arrays. View
code below.

static void Main(string[] args)


{
int i, sum = 0, average, totalAgeBelowAverage = 0;

int youngest = 99;

string message = "";

int numberOfNames;
int yearsLeft;

string retirementMessage = "";

QUESTION 4.1
Prompt for the number of names and then declare the two arrays, called name and age, by
using this input value.

QUESTION 4.2
Once question 4.1 is complete, prompt for the name and age and store these into the parallel arrays,
called name and age.

Page 6 of 8 Compiled by Yiota Moutzouris


QUESTION 4.3
Using the age array, write code to determine the average age and print this out. You need to print out
the number of names.

Also to be printed out is the names of everyone above the average age, as well as the total ages
below the average age.

QUESTION 4.4
Using the age array, determine the youngest age. Variable youngest has been declared.
You need to print out as shown below.

QUESTION 4.5
All names and ages must be printed out, including a specific message via the
retirementMessage variable declared.
If the age is above 55, then “Close to retirement” must be printed out
If the age is above 50, then “Nearly there” must be printed out.

The age gap must also be determined using the ageGap variable. The calculation is 60
minus age.

For all other ages, no message will be printed out.

Page 7 of 8 Compiled by Yiota Moutzouris


QUESTION 4.6
Print each name and age in a message box. View below.
Use the message variable declared to create the message.

You have already calculated the average age previously.


If the age is equal to the average age, print “===”; if the age is above the average age, print
“+++”; otherwise print “---“ if the age is below the average age.

QUESTION 4.7
Declare and use a message1 and message2 variable.
Message1 must show all names and ages less than 40 years of age, while message 2 must
display all names and ages over 40 years of age. Display via messagebox.

message1

message2

Page 8 of 8 Compiled by Yiota Moutzouris

You might also like