Hi, I'm having a little trouble with an assignment for my C# class. (The professor doesn't explain specifics, just the basics, and he isn't any help, he's extremely shy and awkward)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_2_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Inputs = new int[5];
            int intValue = 0;
            float Average = 0;
            float Variance = 0;
            for (int i = 0; i < Inputs.Length; i++)
            {
                while (intValue < 10 || intValue > 50)
                {
                    Console.Clear();
                    Console.WriteLine("Please enter {0} more integers between 10 and 50: ", Inputs.Length - i);
                    intValue = Convert.ToInt32(Console.ReadLine());
                }
                Inputs[i] = intValue;
                intValue = 0;
            }
            Average = (float)(Inputs.Sum() / 5.0f);
            Console.Clear();
            for (int i = 0; i < Inputs.Length; i++)
            {
                Console.WriteLine(Inputs[i]);
                Variance += (Inputs[i] - Average) * (Inputs[i] - Average);
            }
            while ()
            {
                for(i++; < i;)
                    {

                    }
            }
            Console.WriteLine("The average of the integers is {0}\n", Average);
            Variance /= 4;
            Console.WriteLine("The variance of the integers is {0}\n", Variance);
            Console.ReadKey();

        }
    }
}

Here is what I have so far, my current tasks are:
1. (Worth 30 points) Modify your assignment 2 application so that the user’s inputs are stored in an integer array with 5 elements. Compute the mean and variance using the elements of the integer array. Both the mean and variance values should be of type float. Use the following formula for the variance:

  1. (Worth 60 points) Sort the integer elements within the array from lowest (element 0) to highest (element 4). Do not use the preexisting Array.Sort method; code your own. Output the sorted array as follows:

Element 0 => smallest value
Element 1 => next smallest value
Element 2 => next smallest value
Element 3 => next smallest value
Element 4 => Largest value

Suggestions: Use a for() loop inside of a while() loop. Probably the easiest way to code this is to compare array element i with element i+1 in the for() loop. If i+1 < i then swap the values. Be careful not to overflow the array. The while() loop checks whether a swap occurred. When you can go through the array without swapping any values then the array is sorted and you can exit the while() loop.

**For the first assignment, I'm not sure what he's asking? I already have the inputs in an array, but I'm not sure if I have it with 5 elements.
For the second step, I have the for and while loop partially set up, I'm not sure how to swap the values in a for loop or check if there was a swap in the while loop. Thank you very much for any help. **

To answer your first question, yes, you have an array with 5 elements.

int[] Inputs = new int[5];

He is asking you to store the users numbers in the array and do your calculations on them.

Then on the second step you may have a flag (boolean value) that checks whether or not a swap occured in your for loop:

bool swapOccured = true;
while(swapOccured)
{
    swapOccured = false;

    for(int i = 0; i < arr.length;i++)
    {
        for(int j=i+1;j<arr.length;j++)
        {
            if(arr[i] > arr[j])
            swapOccured = true;
            //swap the values
        }
    }
}

I am not going to say how to swap the values because that is for you to figure out.

There is plenty on Google about this, so do a bit more reading up on it and you will get there.

using System;
namespace Whittle.Sorting.Exchange {
    class Bubble<T> :Sorts<T> where T : IComparable {
        override public string Name {
            get { return "Bubble"; }
        }
        override public void Sort(T[] items) {
            int n = items.Length;
            do {
                int newn = 0;
                for (int i = 0; i < n - 1; i++) {
                    if (items[i].CompareTo(items[i + 1]) > 0) {
                        Swap(items, i, i + 1);
                        newn = i + 1;
                    }
                }
                n = newn;
            } while (n > 1);
        }
    }
}

So this is the sorting method I have found, I'm not quite sure how to modify this to work in my program. I have the for/while loops set up to check if the swap occurred, I just don't know where to put the sort and how to change it. 

Here is my current program:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Assignment_3_ITM_225
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] Inputs = new int[5];
            int intValue = 0;
            float Average = 0;
            float Variance = 0;
            for (int i = 0; i < Inputs.Length; i++)
            {
                while (intValue < 10 || intValue > 50)
                {
                    Console.Clear();
                    Console.WriteLine("Please enter {0} more integers between 10 and 50: ", Inputs.Length - i);
                    intValue = Convert.ToInt32(Console.ReadLine());
                }
                Inputs[i] = intValue;
                intValue = 0;
            }
            Average = (float)(Inputs.Sum() / 5.0f);
            Console.Clear();
            for (int i = 0; i < Inputs.Length; i++)
            {
                Console.WriteLine(Inputs[i]);
                Variance += (Inputs[i] - Average) * (Inputs[i] - Average);
            }
            bool swapOccured = true;
            while (swapOccured)
            {
                swapOccured = false;
                for (int i = 0; i < Inputs.Length; i++)
                {
                    for (int j = i + 1; j < Inputs.Length; j++)
                    {
                        if (Inputs[i] > Inputs[j])
                            swapOccured = true;


                    }
                }
            }
            Console.WriteLine("The average of the integers is {0}\n", Average);
            Variance /= 4;
            Console.WriteLine("The variance of the integers is {0}\n", Variance);
            Console.ReadKey();

        }
    }
}

That's it mate, you are nearly there just need to swap the two in your if statement now.

P.s: haven't tested this but I am assuming you have it working..

Edited 3 Years Ago by james6754

This article has been dead for over six months. Start a new discussion instead.