Professional Documents
Culture Documents
Week 3 Exercises - GUDITO, DESSA N. (P1)
Week 3 Exercises - GUDITO, DESSA N. (P1)
BSME-MECH-2 METE280 - P1
WEEK 3A EXERCISES:
Iterative Control Structure
Problem 1: The goals scored in a soccer match may be input as a sequence of 1s and 2s terminated by
0. The number 1 would mean that team 1 made a goal and 2 would mean that team 2 made a goal. Thus
the sequence 1 2 1 2 1 1 2 0 means that Team 1 made 4 goals and team 2 made 3 goals in all. Write a
program that would input such sequence and output how many goals each team made and the result of
the match (“Team 1 won”, “Team 2 won”, “Both tied”). Ensure that inputs are 0,1 and 2 only.
using System;
using System.Threading.Tasks.Dataflow;
namespace Exercise3a1_Soccer
{
class Program
{
static void Main(string[] args)
{
int a = 0, b = 0, score;
Console.WriteLine("\tSOCCER SCORESHEET:\n");
Console.WriteLine("Type\n(1) for Team 1\n(2) for Team 2\n(0) at the end of
tally\n");
do
{
Console.Write("\t");
score = Convert.ToInt32(Console.ReadLine());
if (score == 1)
{
a++;
}
else if (score == 2)
{
b++;
}
else if (score != 2 && score != 0 && score != 1)
{
Console.WriteLine("\nINVALID INPUT");
break;
}
} while (score != 0);
if (score == 0)
{
Console.WriteLine("\nTeam 1: {0}", a);
Console.WriteLine("Team 2: {0}", b);
if (a > b)
{
Console.WriteLine("\nTEAM 1 WON!");
} else if (a < b)
{
Console.WriteLine("\nTEAM 2 WON!");
}
else
{
Console.WriteLine("\nBOTH TIED");
}
}
Console.ReadKey();
}
}
}
Problem 2: Take as input the student’s grades of seven subjects and its corresponding unit taken in a
semester. A student is considered failed if the average grade is less than 3.0. Compute the student’s
General Point Average (GPA) by multiplying the grade per subject by its corresponding unit, sum up the
products and divide the sum by its total units. Print the computed average grade and whether the student
“failed” or “passed”.
using System;
namespace Exercise3a2_GPA
{
class Program
{
static void Main(string[] args)
{
double gpa = 0, grade, sumg = 0, sumunit = 0;
int unit, i;
if (gpa >= 3)
{
Console.WriteLine("PASSED");
}
else
{
Console.WriteLine("FAILED");
}
Console.ReadKey();
}
}
}
WEEK 3B EXERCISES:
Array
Problem 1:
1. Create two sets of integers using array with N number of elements.
Example, given N=4 (four elements of array to input)
set A = {3, 8, 15, 20} // 3,8,15,20 are user inputs
set B = {0, 0, 0, 0} // initialize all elements of this array to 0
namespace Array
{
class Program
{
static void Main(string[] args)
{
int size, i, num, sum=0;
double ave=0;
Console.Write("Input number of elements in array: ");
size = Convert.ToInt32(Console.ReadLine());
int [] a = new int[size];
int [] b = new int[size];
Console.ReadKey();
}
}
}