6 Replies - 477 Views - Last Post: 13 June 2013 - 03:47 PM Rate Topic: -----

#1 FarzanZand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 13

Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 11:19 AM

Hi there! This forum will surely come in handy in for my future career as a programmer, which i just recently noticed that I love. Thanks for being here!

I have been working on this code for about a week and been stuck with the same part last three days.
I can't get the bubble-sort to work!

The task is to write a code that asks for information from 6 sellers, where there are different levels of sallary depending on how many items are sold.. (name, number, district and amount sold).
This information should then be sorted, showing the seller with most sold first, and then writing this information to a file. I have done it all except the actual sorting.

I understand that I should use a bubble array, but I can not seem to get it to work. Not even sure if I have written it correctly. I get no error message or anything, but when I try it out the array is not sorted, showing the one with most sold first. Having an overall hard time understanding functions as well, so that might be a problem.

Could you please lend me a helping hand?

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

namespace Uppgift2
    // kodad av Fredrik Gard och Farzan Lashanizand
{
    public struct säljare
    {
        public String namn;  //name
        public String personNummer; // Social number
        public String distrikt; // district
        public int antal; // amount
        public int nivå; // level
        public int nivåAntal; // amountlevel


    }
    class Program
    {
        //Säljsortering med bubble.sort
        public static säljare[] Bubblesort(säljare[] vektor)
        {
            int bytt = 1;
                säljare temp;
            while (bytt != 0)
            {
                bytt = 0;

                for (int i = 0; i < 5; i++)
                {
                    if (vektor[i].antal > vektor[i + 1].antal)
                    {
                        temp = vektor[i];
                        vektor[i] = vektor[i+1];
                        vektor[i+1] = temp;
                        bytt ++;
                    }
                }
            }
            return vektor;
        }

        //Sellerinformation
        static void Main(string[] args)
        {
            säljare[] säljarInfo = new säljare[6];
            int[] nivåAntal = new int[4];
            //Collecting seller-info
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine("säljare nummer {0}, Vad heter du?", i + 1);
                säljarInfo[i].namn = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Vad är ditt personnummer?", i + 1);
                säljarInfo[i].personNummer = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Vad är ditt distrikt?", i + 1);
                säljarInfo[i].distrikt = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Antal sålda", i + 1);
                säljarInfo[i].antal = Convert.ToInt32(Console.ReadLine());

                //Deiciding levels.
                if (säljarInfo[i].antal < 50) { säljarInfo[i].nivå = 1; nivåAntal[0]++; }
                else if (säljarInfo[i].antal < 100) { säljarInfo[i].nivå = 2; nivåAntal[1]++; }
                else if (säljarInfo[i].antal < 150) { säljarInfo[i].nivå = 3; nivåAntal[2]++; }
                else if (säljarInfo[i].antal >= 200) { säljarInfo[i].nivå = 4; nivåAntal[3]++; }

                //Cancellation of program if a negative number is written.
                if (säljarInfo[i].antal < 0)
                {
                    string omstart;
                    Console.WriteLine("Ditt svar får inte vara ett negativt värde. Vill du starta om programmet? Svara då ja. (ja/nej)");
                    omstart = Console.ReadLine();
                    if (omstart == "Ja" || omstart == "j" || omstart == "JA" || omstart == "ja")
                    {
                        Main(args);
                    }
                    else
                    {
                        Console.WriteLine("Då du inte svarade ja, kommer programmet att avslutas.");
                        Console.ReadLine(); Environment.Exit(0);
                    }
                }
            }


            //Writing to file
            using (StreamWriter writer = new StreamWriter("säljardata.txt"))
            {
                //Presentation of levels.
                Console.WriteLine("Antal som har nått respektive nivå");
                if (nivåAntal[0] > 0) { Console.WriteLine("Nivå 1: " + nivåAntal[0] + " stycken."); }
                if (nivåAntal[1] > 0) { Console.WriteLine("Nivå 2: " + nivåAntal[1] + " stycken."); }
                if (nivåAntal[2] > 0) { Console.WriteLine("Nivå 3: " + nivåAntal[2] + " stycken."); }
                if (nivåAntal[3] > 0) { Console.WriteLine("Nivå 4: " + nivåAntal[3] + " stycken."); }
                Console.WriteLine();
                Console.WriteLine("Namn Personnummer Distrikt Antal");

                writer.WriteLine("Antal som har nått respektive nivå");
                if (nivåAntal[0] > 0) { writer.WriteLine("Nivå 1: " + nivåAntal[0] + " stycken."); }
                if (nivåAntal[1] > 0) { writer.WriteLine("Nivå 2: " + nivåAntal[1] + " stycken."); }
                if (nivåAntal[2] > 0) { writer.WriteLine("Nivå 3: " + nivåAntal[2] + " stycken."); }
                if (nivåAntal[3] > 0) { writer.WriteLine("Nivå 4: " + nivåAntal[3] + " stycken."); }
                writer.WriteLine();
                writer.WriteLine("Namn Personnummer Distrikt Antal");
                for (int i = 0; i < 6; i++)
                {

                    Console.WriteLine(säljarInfo[i].namn + " " + säljarInfo[i].personNummer + " " + säljarInfo[i].distrikt + " " + säljarInfo[i].antal);


                    writer.WriteLine(säljarInfo[i].namn + " " + säljarInfo[i].personNummer + " " + säljarInfo[i].distrikt + " " + säljarInfo[i].antal);
                }
            }
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Help please. Creating a list of sellers and using bubble sort.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6062
  • View blog
  • Posts: 23,513
  • Joined: 23-August 08

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 11:21 AM

This looks like C#, not C++. Moved to C#.
Was This Post Helpful? 0
  • +
  • -

#3 FarzanZand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 13

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 12:11 PM

Oh, sorry about that. Still learning to navigate my way around. Won't happen again. :)/>


I have a feeling that the bubble-sort code should have my array-name säljarinfo instead of vektor. (Got vektor from a book). Tried calling the method with Bubblesort(säljarinfo); but that did not work either. Just says " the name does not exist in its current context".

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

namespace Uppgift2
    // kodad av Fredrik Gard och Farzan Lashanizand
{
    public struct säljare
    {
        public String namn;  //name
        public String personNummer; // Social number
        public String distrikt; // district
        public int antal; // amount
        public int nivå; // level
        public int nivåAntal; // amountlevel

        
    }
    class Program
    {
        // bubblesort


        public static säljare[] Bubblesort(säljare[] säljarinfo)
        {
            int bytt = 1;
                säljare temp;
            while (bytt != 0)
            {
                bytt = 0;

                for (int i = 0; i < 5; i++)
                {
                    if (säljarinfo[i].antal > säljarinfo[i + 1].antal)
                    {
                        temp = säljarinfo[i];
                        säljarinfo[i] = säljarinfo[i+1];
                        säljarinfo[i+1] = temp;
                        bytt ++;
                    }
                }
            }
            return säljarinfo;
        }

        //Sellerinformation
        static void Main(string[] args)
        {
            säljare[] säljarInfo = new säljare[6];
            int[] nivåAntal = new int[4];
            //Collecting seller-info
            for (int i = 0; i < 6; i++)
            {
                Console.WriteLine("säljare nummer {0}, Vad heter du?", i + 1);
                säljarInfo[i].namn = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Vad är ditt personnummer?", i + 1);
                säljarInfo[i].personNummer = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Vad är ditt distrikt?", i + 1);
                säljarInfo[i].distrikt = Console.ReadLine();

                Console.WriteLine("säljare nummer {0}, Antal sålda", i + 1);
                säljarInfo[i].antal = Convert.ToInt32(Console.ReadLine());



                //Deiciding levels.
                if (säljarInfo[i].antal < 50) { säljarInfo[i].nivå = 1; nivåAntal[0]++; }
                else if (säljarInfo[i].antal < 100) { säljarInfo[i].nivå = 2; nivåAntal[1]++; }
                else if (säljarInfo[i].antal < 150) { säljarInfo[i].nivå = 3; nivåAntal[2]++; }
                else if (säljarInfo[i].antal >= 200) { säljarInfo[i].nivå = 4; nivåAntal[3]++; }

                //Cancellation of program if a negative number is written.
                if (säljarInfo[i].antal < 0)
                {
                    string omstart;
                    Console.WriteLine("Ditt svar får inte vara ett negativt värde. Vill du starta om programmet? Svara då ja. (ja/nej)");
                    omstart = Console.ReadLine();
                    if (omstart == "Ja" || omstart == "j" || omstart == "JA" || omstart == "ja")
                    {
                        Main(args);
                    }
                    else
                    {
                        Console.WriteLine("Då du inte svarade ja, kommer programmet att avslutas.");
                        Console.ReadLine(); Environment.Exit(0);
                    }
                }
            }
            Bubblesort(säljarinfo);

            //Writing to file
            using (StreamWriter writer = new StreamWriter("säljardata.txt"))
            {

                //Presentation of levels.
                Console.WriteLine("Antal som har nått respektive nivå");
                if (nivåAntal[0] > 0) { Console.WriteLine("Nivå 1: " + nivåAntal[0] + " stycken."); }
                if (nivåAntal[1] > 0) { Console.WriteLine("Nivå 2: " + nivåAntal[1] + " stycken."); }
                if (nivåAntal[2] > 0) { Console.WriteLine("Nivå 3: " + nivåAntal[2] + " stycken."); }
                if (nivåAntal[3] > 0) { Console.WriteLine("Nivå 4: " + nivåAntal[3] + " stycken."); }
                Console.WriteLine();
                Console.WriteLine("Namn Personnummer Distrikt Antal");

                writer.WriteLine("Antal som har nått respektive nivå");
                if (nivåAntal[0] > 0) { writer.WriteLine("Nivå 1: " + nivåAntal[0] + " stycken."); }
                if (nivåAntal[1] > 0) { writer.WriteLine("Nivå 2: " + nivåAntal[1] + " stycken."); }
                if (nivåAntal[2] > 0) { writer.WriteLine("Nivå 3: " + nivåAntal[2] + " stycken."); }
                if (nivåAntal[3] > 0) { writer.WriteLine("Nivå 4: " + nivåAntal[3] + " stycken."); }
                writer.WriteLine();
                writer.WriteLine("Namn Personnummer Distrikt Antal");
                for (int i = 0; i < 6; i++)
                {

                    Console.WriteLine(säljarInfo[i].namn + " " + säljarInfo[i].personNummer + " " + säljarInfo[i].distrikt + " " + säljarInfo[i].antal);


                    writer.WriteLine(säljarInfo[i].namn + " " + säljarInfo[i].personNummer + " " + säljarInfo[i].distrikt + " " + säljarInfo[i].antal);
                }
            }
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 12:15 PM

Just a quick look over the code I see that you use (line 053) a for loop from 0 to less than 6, but in your sort (line 033) you use from 0 to less than 5, ignoring the last element of your array.
Was This Post Helpful? 0
  • +
  • -

#5 FarzanZand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 13

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 12:21 PM

Now that was a silly mistake, thanks for catching it mate. changed it to a 6.

Any idea though how I can get the bubble-sort to kick in and sort my array? Can't seem to get it to kick in, and having trouble calling it.
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,524
  • Joined: 08-April 09

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 01:44 PM

You called it on line 94 but you didn't store the result anywhere, if your method returns something you need someplace to store that result.
Was This Post Helpful? 0
  • +
  • -

#7 FarzanZand  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 10-June 13

Re: Help please. Creating a list of sellers and using bubble sort.

Posted 13 June 2013 - 03:47 PM

Thanks alot guys! I got it to work now. I appreciate the help. :)

Perhaps there is a future in coding for me after all! Just need to keep to it and work hard and I have never had so much fun with any other subject before. Wish I just discovered programming sooner. :)

Cheers :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1