Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,106 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,719 people online right now. Registration is fast and FREE... Join Now!




Parallel Arrays

 
Reply to this topicStart new topic

Parallel Arrays, Help with codings!

kelliethile
23 Nov, 2007 - 09:46 AM
Post #1

New D.I.C Head
*

Joined: 22 Oct, 2007
Posts: 14


My Contributions
Given x, a weight selected by the user, write the code required to:
a) Calculate how many kids weight x pounds.
cool.gif 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
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Parallel Arrays
23 Nov, 2007 - 10:35 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Quick question: is the "parallel arrays" thing required? It's an awful way to do it. Just a class and a list would get you so much closer to what you're aiming for.

CODE

class Kid {
   public int age;
   public int weight;
   public string name;
   public Kid() {}
   public Kid(int age, int weight, string name) {
      this.age = age;
      this.weight = weight;
      this.name = name;
   }
}

List <Kid> kids = new List <Kid>();
kids.Add(new Kid(2,20,"John"));
kids.Add(new Kid(1,29,"Emma"));


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:20PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month