Given x, a weight selected by the user, write the code required to:
a) Calculate how many kids weight x pounds.

List the names of all kids whose weight is x.
This program was written to compare Abby's age with the other kids listed in the program. I am trying to write a program that allows users to pick a selected weight, then calculate how many kids weight the selected weight, then list their names. I have yet to write that part of codes. I ask for help with codings and suggestions. Thanks!
EDIT: I wrote the codes. Can you check for errors? There are three of them. I can't fix it.
//Problem 2: TO DO
//Given x, a weight selected by the user, write the code snippet to:
//1. Calculate how many kids weight x pounds.
//2. List the names of all kids whose weight is x.
//
//PART A: Find x's weight
int x;
Console.Out.Write("Enter your desired weight from 18-23 lbs: ");
x = int.Parse(Console.In.ReadLine());
// Step 1: find index where "x" occurs in weight[] array...
index = search( weight, x); //return index where "x" occurs in weight[]
if (index == -1) //if weight not found at all
{
Console.Out.WriteLine( "Weight not found");
return;
}
// Step 2: find who's whose weight
int kidWeight; //holds x's age
int kidName;
kidWeight = weight[ index]; //use same index to get age associated with "x"
Console.Out.WriteLine("These kids who weight" + kidWeight + "lbs are: " + kidName);
//PART B:
//count number of occurences of "kidWeight" (x's weight) in weight[] array
//holds number of occurrences
times = count( age, kidWeight);
Console.Out.WriteLine( times + "kids weights in as " + kidWeight + "lbs.");
//CODE
/* Project: ParallelArrays
* File: Program.cs
* Author: KTL
* Date: Fall 2007
*
* Project Description:
* - This project illustrates the use of "parallel arrays".
*/
using System;
using System.Collections.Generic;
using System.Text;
namespace ArraysNotFull
{
class Program
{
static void Main(string[] args)
{
//Parallel arrays associate each entry in multiple arrays with the same index.
//for instance: Kathy (index 2 in name[] array) is
// 2 years old (index 2 in age[] array), and
// weighs 18 lbs (index 2 in weight[] array).
int[] age = { 3, 1, 2, 5, 3, 2, 4, 6 };
int[] weight = { 20, 19, 18, 20, 21, 22, 23, 20 };
String[] name = { "John", "Emma", "Kathy", "Susy", "Abby", "Lia", "Beth", "Mike" };
//Problem 1: How many kids have the same age as "Abby"?
//Solution: PART A: Find Abby's age, then
// PART B: Count number of times Abby's age appears in age[] array
//PART A: Find Abby's age
// Step 1: find index where "Abby" occurs in name[] array...
int index;
index = search( name, "Abby"); //return index where "Abby" occurs in name[]
if (index == -1) //if name not found at all
{
Console.Out.WriteLine( "Abby not found");
return;
}
// Step 2: find Abby's age
int kidAge; //holds Abby's age
kidAge = age[ index]; //use same index to get age associated with "Abby"
Console.Out.WriteLine( "Abby's age is " + kidAge);
//PART B:
//count number of occurences of "kidAge" (Abby's age) in age[] array
int times; //holds number of occurrences
times = count( age, kidAge);
Console.Out.WriteLine( times + " kids have the same age as Abby");
//Problem 2: TO DO
//Given x, a weight selected by the user, write the code snippet to:
//1. Calculate how many kids weight x pounds.
//2. List the names of all kids whose weight is x.
//
//PART A: Find x's weight
int x;
Console.Out.Write("Enter your desired weight from 18-23 lbs: ");
x = int.Parse(Console.In.ReadLine());
// Step 1: find index where "x" occurs in weight[] array...
index = search( weight, x); //return index where "x" occurs in weight[]
if (index == -1) //if weight not found at all
{
Console.Out.WriteLine( "Weight not found");
return;
}
// Step 2: find who's whose weight
int kidWeight; //holds x's age
int kidName;
kidWeight = weight[ index]; //use same index to get age associated with "x"
Console.Out.WriteLine("These kids who weight" + kidWeight + "lbs are: " + kidName);
//PART B:
//count number of occurences of "kidWeight" (x's weight) in weight[] array
//holds number of occurrences
times = count( age, kidWeight);
Console.Out.WriteLine( times + "kids weights in as " + kidWeight + "lbs.");
// } //main() method
static int search(String[] Arr, String name)
{ //returns index where 'name' is found in the array of strings 'Arr'...
for (int i = 0; i < Arr.Length; i++)
{
if (name.Equals(Arr[i])) //This is the correct way of checking
return i; // whether two Strings are equal.
} // Remember: Strings are objects!
return -1; //name not found
} //search() method
static int count(int[] Arr, int num)
{ //returns number of times 'num' occurs in the arrays of integers 'Arr'...
int times = 0; //number of occurrences of "num" in Arr
for (int i = 0; i < Arr.Length; i++)
{
if (num == Arr[i]) //in this case we can use the regular '=='
times = times + 1; // since we're comparing integers. We can
} // compare primitive datatypes using '=='
return times;
} //count() method
} //Program class
} //ArraysNotFull project
This post has been edited by kelliethile: 23 Nov, 2007 - 10:05 AM