Skip to content

Homework help did i do it right

Jenni Witzel

Asked Oct 25, 2008, 2:25 PM EDT4 answers
Share
LinkedInX
PowerShell
/********************************************************/
/*   Your Name                                          */
/*   CIS 218                                            */
/*   LAB 8 - Working with an array                      */
/*   Due Date: xx/xx/xxxx                               */
/*                                                     
*/
/*   Description: This program prompts for 12
integers, */
/*                prints a list of them as well
as the  */
/*                largest and smallest values.
Data is  */
/*                stored in an array.                   */
/*                                                     
*/
/********************************************************/
using System;
using SC =
System.Console;
public class
FLastLab8
{ //Main Method
    public static void Main()
    {
        int[] myList = new int[12];

        SC.WriteLine("Lab 8 – Your Name\n");

        for (int x = 0; x < 12; ++x)
        {
            SC.Write("Please enter a value
for element {0} -->\t", x);
            myList[x] =
Convert.ToInt32(SC.ReadLine());
        }



 

 


        SC.WriteLine(“\n\nThe values entered were: {0}”, myList[0]);



 

 

 

 


      

       

 

       

 



 

 

 

 

 

 


Code
        SC.WriteLine("\nLab 8 has
successfully terminated for Your Name");
    }
}


Ok this is what i have so far


/********************************************************/
/*  
Your Name                                          */
/*   CIS 218                                            */
/*   LAB 8 - Working with an array                      */
/*   Due Date: 10/23/2008                               */
/*                                                      */
/*   Description: This program prompts for 12 integers, */
/*                prints a list of them as well as the  */
/*                largest and smallest values. Data is  */
/*                stored in an array.                   */
/*                                                      */
/********************************************************/
using System;

using SC = System.Console;

public class NLastLab8
{ //Main Method

    public static void Main()
    {

        int[] myListArray = new int[12];

        myListArray[0] = myListArray[1] = 1;

        SC.WriteLine("Lab 8 – Your Name\n");

        for (int x = 0; x < 12; ++x)
        {

            SC.Write("Please enter a value for element {0} -->\t", x);

            myListArray[x] = Convert.ToInt32(SC.ReadLine());

        }


                SC.WriteLine("\n\nThe last value entered was: " + myListArray[myListArray.Length - 1]);


                Array.Sort(myListArray);


                ("The smallest value entered was: {0} -->" myListArray[0]);

                for (int i = myListArray.Length - 1; i >= 0; i--)
            {

                SC.WriteLine(myListArray[i]);

            }


                ("The largest value entered was: {0} -->", myListArray[11]);

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

            SC.WriteLine(myListArray[i]);

        }

        SC.WriteLine("\nLab 8 has successfully terminated for Your Name");


        Array.Sort(myListArray);

    }

}


The teacher says

Array.Sort(myList);

Once the arrary is sorted, the largest value will be in the last position of the array (subscript value of 11) and the smallest element will be in the first position (subscript value of 0).

Here is how you can print the largest element:
SC.WriteLine("largest value entered is{0}", myList[11]);

Here is how you can print the smallest element:
SC.WriteLine("smallest value entered is{0}", myList[0]);


Did i do this right?

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Alex Rivenbark
    +1

    Ok Jenni you have a couple of issues with your output statements. any time that you want a line to be printed to the console, you must use the Console.WriteLine() method. Also, to keep the console from disappearing immediately after opening, you must use the Console.ReadKey() methlod. The corrected code is below. Don't forget to accept this answer if it helps.

     

    C#
    using System;
    using SC = System.Console;
    public class NLastLab8
    { //Main Method
    public static void Main()
    {
    int[] myListArray = new int[12];
    myListArray[0] = myListArray[1] = 1;
    SC.WriteLine("Lab 8 – Your Name\n");
    for (int x = 0; x < 12; ++x)
    {
    SC.Write("Please enter a value for element {0} -->\t", x);
    myListArray[x] = Convert.ToInt32(SC.ReadLine());
    }
    
    SC.WriteLine("\n\nThe last value entered was: " + myListArray[myListArray.Length - 1]);
    
    Array.Sort(myListArray);
    
    SC.WriteLine("The smallest value entered was: {0} -->", myListArray[0]);
    for (int i = myListArray.Length - 1; i >= 0; i--)
    {
    SC.WriteLine(myListArray[i]);
    }
    
    SC.WriteLine("The largest value entered was: {0} -->", myListArray[11]);
    for (int i = 0; i < myListArray.Length; i++)
    {
    SC.WriteLine(myListArray[i]);
    }
    SC.WriteLine("\nLab 8 has successfully terminated for Your Name");
    SC.ReadKey();
    
    Array.Sort(myListArray);
    }
    }
    +1
  • Alex Rivenbark
    +1

    Jenni, i'm sorry, but I just got your reply to the post before this one. Since I now know the criteria, the code that you need is as follows. Please make sure that you  understand it, so you can learn it. If you have any quesitons, ask. Don't forget to accept this answer. Take care.

    C#
    using System;
    using SC = System.Console;
    public class NLastLab8
    { //Main Method
    public static void Main()
    {
    int[] myListArray = new int[12];
    myListArray[0] = myListArray[1] = 1;
    SC.WriteLine("Lab 8 – Your Name\n");
    for (int x = 0; x < 12; ++x)
    {
    SC.Write("Please enter a value for element {0} -->\t", x);
    myListArray[x] = Convert.ToInt32(SC.ReadLine());
    }
    SC.WriteLine("\n\nThe last value entered was: " + myListArray[11]);
    for (int i = 0; i < myListArray.Length; i++)
    {
    SC.WriteLine(myListArray[i]);
    }
    Array.Sort(myListArray);
    SC.WriteLine("The smallest value entered was: {0} -->", myListArray[0]);
    SC.WriteLine("The largest value entered was: {0} -->", myListArray[11]);
    SC.WriteLine("\nLab 8 has successfully terminated for Your Name");
    SC.ReadKey();
    Array.Sort(myListArray);
    }
    }
    +1
  • +1
    I understand what i did wrong but my teacher does not help us at all. So we have to figure everthg out on our own. Which really sucks. Thanks for the help did you see what he gave us and he told us to fig out the rest on own I was like wtf. I have no idea what I am doing.
    +1
  • Alex Rivenbark
    +1

    Yeah. That's the way it goes sometimes with online courses. Remember, one of the most important things about programming is understanding the problem. After that, you can usually find the correct way to implement a usable solution. Some good resources are your text book, Google, or forums such as this. By the way, are you using an IDE such as Visual Studio to code and debug these labs?

    +1