Sunteți pe pagina 1din 14

GUDITO, DESSA N.

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;

for(i=0; i<7; i++)


{
Console.WriteLine("\nSubject No. {0}", i+1);
Console.Write("\tGrade: ");
grade = Convert.ToDouble(Console.ReadLine());
Console.Write("\tNo. of Units: ");
unit = Convert.ToInt32(Console.ReadLine());
sumg = sumg + (grade * unit);
sumunit = sumunit + unit;
}
gpa = (double)sumg / sumunit;
Console.Write("\n\nGeneral Point Average (GPA): {0:0.00}\t", gpa);

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

After creating the array, solve the following:


A. Ask an integer input from the user and search the value if it is an element of the array (set A) and
display the index of the integer.
B. Compute the average of the elements in the array (set A).
C. Copy the elements of the first array (set A) to the second array (set B). Ex.set B = {3,8,15,20}
D. Display the reverse of the second array (set B). Ex. set B = {20, 15, 8, 3}
using System;

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.WriteLine("\n\tInput {0} integer(s):", size);


for (i=0; i<size; i++)
{
Console.Write("\t ");
a[i] = Convert.ToInt32(Console.ReadLine());
b[i] = 0;
}

Console.Write("\tInput an integer: ");


num = Convert.ToInt32(Console.ReadLine());
for(i=0; i<size; i++)
{
if(num == a[i])
{
Console.WriteLine("\t{0} is an element of the array, located at index
{1}", num, i);
break;
}else if (num != a[i] && i == size - 1)
{
Console.WriteLine("\t{0} is not an element of the array", num);
}
}

for(i=0; i<size; i++)


{
sum = sum + a[i];
}
ave = (double)sum / size;
Console.WriteLine("\n AVERAGE: {0:0.00}", ave);

Console.Write("DUPLICATE: set B = { ");


for (i = 0; i < size; i++)
{
b[i] = a[i];
Console.Write("{0} ", b[i]);
}
Console.WriteLine("}");

Console.Write(" REVERSE: set B = { ");


for (i = 0; i < size; i++)
{
b[i] = a[size-1-i];
Console.Write("{0} ", b[i]);
}
Console.Write("}");

Console.ReadKey();
}
}
}

S-ar putea să vă placă și