Jump to content

C# Project; Index two arrays in a method

Go to solution Solved by vorticalbox,
6 hours ago, Rain7 said:

static void Main(string[] args)
{
  decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
  int[] yearsOfExperience = new int[6];

  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    Console.WriteLine("Enter the years of Experience: ");
    yearsOfExperience[i] = int.Parse(Console.ReadLine());
  }

  Console.WriteLine("Total salary is: " + CalculateTotalSalaries(minimumSalary, yearsOfExperience));
  Console.ReadKey();
}

private static decimal CalculateTotalSalaries(decimal[] minimumSalary, int[] yearsOfExperience)
{
  decimal totalSalary = 0;
  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    for (int j = 0; j < minimumSalary.Length; j++)
    {
      if (yearsOfExperience[i] == j)
      {
        totalSalary += minimumSalary[j];
        Console.WriteLine(minimumSalary[j]);
        break;
      }
    }
  }
  return totalSalary;
}

 

You don't need to loop both arrays just the years of experience 

namespace Project_6
{
    class Program
    {
        //Name: Adam Rubin
        //Date Created: 4/9/2018
        //Purpose: To create a program that helps the user figure out if they can hire teachers.
        static void Main(string[] args)
        {
            //Declare arrays:
            //Minimum Salary Array:
            decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
            //Years of Experience Array (Entry)
            int[] yearsOfExperience = new int[6];


            //Request user input for 6 teachers, and their years of experience
            for (int i = 0; i < yearsOfExperience.Length; i++)
            {
                Console.WriteLine("Enter the years of Experience: ");
                yearsOfExperience[i] = int.Parse(Console.ReadLine());
            }


            //console.readkey to keep everything on the screen
            Console.ReadKey();

        }//end of main

        private static decimal CalculateTotalSalaries(decimal[] decimalArray, decimal[] decimalArray2)
        {
            decimal totalSalary = 0;
            for (int i = 0; i < decimalArray.Length; i++)
            {
                totalSalary = totalSalary + decimalArray[i];
            }
            return totalSalary;
        }
    }
}

Hey everyone, hit a roadblock. I've basically got to take two arrays, one with total years of experience that the user enters, and one that has the minimum salary. Basically what needs to happen in the method CalculateTotalSalaries is for the total of salaries per years of what the user entered.

 

i.e. what would happen is that when the user enters:

1

1

5

3

6

2

the method would add together

31975

31975

33500

32725

33900

32350

and spit out the return value as: 196425 for total salary

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry, real quickly I did change the method header to: 

private static decimal CalculateTotalSalaries(decimal[] decimalArray, int[] intArray)

as the years of experience array is supposed to be an int.

Link to comment
Share on other sites

Link to post
Share on other sites

Where are you calling CalculateTotalSalaries? I'm a little confused as to what each of the 4 parameters are for in that function.

What I would do is create an array of all the salaries and then use the C# built in function to sum them:

int sum = arr.Sum();

 

My main computer:

i7 6700k || GTX 1070 || Asus Z170 RGB || C.M. Hyper 212 EVO || 16GB RAM || 256GB NVMe SSD || 500GB SATA SSD || 12TB total HDD || Define R5 Blackout || 850W PSU

More Details Below :) 

Spoiler

 

CPU: Intel Core i7 6700k                             GPU:  EVGA GTX 1070 FTW                                  |  Motherboard: Asus Z170 Pro Gaming Aura
CPU Cooler: C.M. Hyper 212 EVO             RAM: 16GB Kingston Fury 4x4 DDR4 2400MHz         SSD:  Intel 256GB NVMe SSD & Plextor 500GB SATA SSD

Hard Drive:  WD 2TB Black, 2TB WD Blue, 8TB WD Red     Case:  Fractal Design Define R5 Blackout Edition   PSU:  Be Quiet! Dark Power Pro 11 850W

 

Additional Computer Parts: NZXT Hue for LEDs

Peripherals: Dell U2414H (x2) || Corsair Sabre RGB || Corsair K95 Platinum || Sennheiser 558's || Modmic

 

Pictures of setup:

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, njmyers3 said:

Where are you calling CalculateTotalSalaries? I'm a little confused as to what each of the 4 parameters are for in that function.

What I would do is create an array of all the salaries and then use the C# built in function to sum them:


int sum = arr.Sum();

 

Sorry, hadn't called the CalculateTotalSalary yet. 

It now looks like this with the method called:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project_6
{
    class Program
    {
        //Name: Adam Rubin
        //Date Created: 4/9/2018
        //Purpose: To create a program that helps the user figure out if they can hire teachers.
        static void Main(string[] args)
        {
            //Declare arrays:
            //Minimum Salary Array:
            decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
            //Years of Experience Array (Entry)
            int[] yearsOfExperience = new int[6];


            //Request user input for 6 teachers, and their years of experience
            for (int i = 0; i < yearsOfExperience.Length; i++)
            {
                Console.WriteLine("Enter the years of Experience: ");
                yearsOfExperience[i] = int.Parse(Console.ReadLine());
            }

            //Declare totalSalary
            decimal totalSalary;
            //pass the two arrays to the CalculateTotalSalaries array
            totalSalary = CalculateTotalSalaries(minimumSalary, yearsOfExperience);
            
            //Console.readkey to keep everything on the screen
            Console.ReadKey();

        }//end of main

        private static decimal CalculateTotalSalaries(decimal[] decimalArray, int[] intArray)
        {
            decimal totalSalary = 0;
            for (int i = 0; i < decimalArray.Length; i++)
            {
                totalSalary = totalSalary + decimalArray[i];
            }
            return totalSalary;
        }
    }
}

If I used the .sum() function, wouldn't it just sum all of the values within the array that I pass to it?

What I'm looking to do is enter a bunch of different years of experience values to an array, and then pass that along with a array of minimum salaries into a method and add the salaries together based on minimum experience. 

Sorry if I'm not explaining it too well -- still kinda bad at programming. 

Link to comment
Share on other sites

Link to post
Share on other sites

decimal total = 0;

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

total += decimalArray[intArray[i-1]];

}

return total;

Sorry, I haven't worked with C languages for a while. I hope that this will help you. Basically, it creates an variable for the total salary and loops through each number of years. It then gets the index of the years worked and subtracts one (because indexing starts at 0). Finally, it gets the salary based on the the new index number of [index-1].

I'd recommend using more specific variable names for the future and keep in mind that you can use the same variable names within different scopes, so your function parameters can be the same as the input parameters.

Hope this helps you! :) 

My main computer:

i7 6700k || GTX 1070 || Asus Z170 RGB || C.M. Hyper 212 EVO || 16GB RAM || 256GB NVMe SSD || 500GB SATA SSD || 12TB total HDD || Define R5 Blackout || 850W PSU

More Details Below :) 

Spoiler

 

CPU: Intel Core i7 6700k                             GPU:  EVGA GTX 1070 FTW                                  |  Motherboard: Asus Z170 Pro Gaming Aura
CPU Cooler: C.M. Hyper 212 EVO             RAM: 16GB Kingston Fury 4x4 DDR4 2400MHz         SSD:  Intel 256GB NVMe SSD & Plextor 500GB SATA SSD

Hard Drive:  WD 2TB Black, 2TB WD Blue, 8TB WD Red     Case:  Fractal Design Define R5 Blackout Edition   PSU:  Be Quiet! Dark Power Pro 11 850W

 

Additional Computer Parts: NZXT Hue for LEDs

Peripherals: Dell U2414H (x2) || Corsair Sabre RGB || Corsair K95 Platinum || Sennheiser 558's || Modmic

 

Pictures of setup:

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, njmyers3 said:

decimal total = 0;

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

total += decimalArray[intArray[i-1]];

}

return total;

Sorry, I haven't worked with C languages for a while. I hope that this will help you. Basically, it creates an variable for the total salary and loops through each number of years. It then gets the index of the years worked and subtracts one (because indexing starts at 0). Finally, it gets the salary based on the the new index number of [index-1].

I'd recommend using more specific variable names for the future and keep in mind that you can use the same variable names within different scopes, so your function parameters can be the same as the input parameters.

Hope this helps you! :) 

It's getting me closer! I have an error -- whenever it hits the i-1 part, it just fails saying that it's out of the bounds of the array. Whenever I put, just i though it works. I'm going to work it up to it decreasing down to 0 with 1, which won't be something the array can index to. I makes it work fine though. Thanks for your help!!

Link to comment
Share on other sites

Link to post
Share on other sites

Sorry about spacing I am on my phone


decimal total =0

for(var i = 0; i <= yearsOfExperience.length; i ++)

{

  total += minimumSalary[yearsOfExperience[i]]

}

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

static void Main(string[] args)
{
  decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
  int[] yearsOfExperience = new int[6];

  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    Console.WriteLine("Enter the years of Experience: ");
    yearsOfExperience[i] = int.Parse(Console.ReadLine());
  }

  Console.WriteLine("Total salary is: " + CalculateTotalSalaries(minimumSalary, yearsOfExperience));
  Console.ReadKey();
}

private static decimal CalculateTotalSalaries(decimal[] minimumSalary, int[] yearsOfExperience)
{
  decimal totalSalary = 0;
  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    for (int j = 0; j < minimumSalary.Length; j++)
    {
      if (yearsOfExperience[i] == j)
      {
        totalSalary += minimumSalary[j];
        Console.WriteLine(minimumSalary[j]);
        break;
      }
    }
  }
  return totalSalary;
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, Rain7 said:

static void Main(string[] args)
{
  decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
  int[] yearsOfExperience = new int[6];

  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    Console.WriteLine("Enter the years of Experience: ");
    yearsOfExperience[i] = int.Parse(Console.ReadLine());
  }

  Console.WriteLine("Total salary is: " + CalculateTotalSalaries(minimumSalary, yearsOfExperience));
  Console.ReadKey();
}

private static decimal CalculateTotalSalaries(decimal[] minimumSalary, int[] yearsOfExperience)
{
  decimal totalSalary = 0;
  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    for (int j = 0; j < minimumSalary.Length; j++)
    {
      if (yearsOfExperience[i] == j)
      {
        totalSalary += minimumSalary[j];
        Console.WriteLine(minimumSalary[j]);
        break;
      }
    }
  }
  return totalSalary;
}

 

You don't need to loop both arrays just the years of experience 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

45 minutes ago, vorticalbox said:

You don't need to loop both arrays just the years of experience 

Good catch, I've refactored the code:

static void Main(string[] args)
{
  decimal[] minimumSalary = new decimal[] { 31600M, 31975M, 32350M, 32725M, 33100M, 33500M, 33900M };
  int[] yearsOfExperience = new int[6];

  for (int i = 0; i < yearsOfExperience.Length; i++)
  {
    Console.WriteLine("Enter the year(s) of Experience: ");
    string lineAnswer = Console.ReadLine();
    int lineInt;

    if (int.TryParse(lineAnswer, out lineInt) && lineInt >= 0)
    {
      TakeAnswer(yearsOfExperience, i, lineInt);
    }

    else
    {
      while (true)
      {
        Console.WriteLine("Enter a whole number greater than or equal to 0: ");
        lineAnswer = Console.ReadLine();

        if (int.TryParse(lineAnswer, out lineInt) && lineInt >= 0)
        {
          TakeAnswer(yearsOfExperience, i, lineInt);
          break;
        }
      }
    }
  }

  Console.WriteLine("Total salary is: " + CalculateTotalSalaries(minimumSalary, yearsOfExperience));
  Console.ReadKey();
}

private static decimal CalculateTotalSalaries(decimal[] minimumSalary, int[] yearsOfExperience)
{
  decimal total = 0;

  for (var i = 0; i < yearsOfExperience.Length; i++)
  {
    if(yearsOfExperience[i] <= yearsOfExperience.Length)
    {
      total += minimumSalary[yearsOfExperience[i]];
    }
    else
    {
      total += minimumSalary[yearsOfExperience.Length];
    }
  }

  return total;
}

static void TakeAnswer(int[] arrayToAppendTo, int loopIndex, int numberOfYears)
{
  arrayToAppendTo[loopIndex] = numberOfYears;
}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×