Question

I cannot figure out why my removeAll for var = 7 is not working.

C Microsoft Visual Studio Debug Console 29 33 49 Removed 29 12 33 Removed 49 12 All 7s Removed Maximum is: 7 Minimum is: 2 So

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using UnorderedArrayListNamespace;

namespace Array

{

public class Program

{

public static void Main(string[] args)

{

UnorderedArrayList u = new UnorderedArrayList();

u.print();
int var = 5;
u.insert(ref var);
var = 12;
u.insert(ref var);
var = 2;
u.insert(ref var);
var = 29;
u.insert(ref var);
var = 7;
u.insert(ref var);
var = 33;
u.insert(ref var);
var = 49;
u.insert(ref var);
var = 7;
u.insert(ref var);
var = 3;
u.insert(ref var);
Console.WriteLine("Starting");
u.print();
Console.WriteLine("Removed 29");
var = 29;
u.remove(ref var);
u.print();
Console.WriteLine("Removed 49");
var = 49;
u.remove(ref var);
u.print();
Console.WriteLine("All 7s Removed");
var = 7;
u.removeAll(ref var);
u.print();
Console.WriteLine("Maximum is: " + u.max());

Console.WriteLine("Minimum is: " + u.min());

u.insertionSort();

Console.WriteLine("Sorted");

u.print();

}

}

}
namespace UnorderedArrayListNamespace
{
public class UnorderedArrayList
{
protected int[] list;
protected int next;
public UnorderedArrayList()
{
list = new int[100];
next = 0;
}
public void insert(ref int item)

{

list[next] = item;

next++;

}
public void remove(ref int item)

{
int i;

if (next == 0)

{
Console.WriteLine("Empty List");
}

if (item.Equals(list[0]))
{
list[0] = list[--next];
}
else
{   
for (i = 0; i < next - 1; i++)
{

if (item.Equals(list[i]))

{
for (int j = i; j < next; j++)
list[j] = list[j + 1];
next--;
break;
}

}

if (item.Equals(list[next - 1]) && i == next - 1)

next--;
}
}
public void removeAll(ref int item)
{
if (next == 0)

{
Console.WriteLine("Empty List");
}
else
{
if (item.Equals(list[0]))

list[0] = list[--next];

for (int i = 1; i < next; i++)
{
if (item.Equals(list[i]))
{
if (i == next - 1)
next--;
}
else
{
for (int j = i; j < next; j++)
list[j] = list[j + 1];
next--;
}
}
}
}
public int min()
{
int x = list[0];
for (int i = 1; i < next; i++)

{
if (x > list[i])
x = list[i];
}
return x;
}
public int max()
{
int x = list[0];

for (int i = 1; i < next; i++)

{
if (x < list[i])
x = list[i];
}
return x;
}
public void insertionSort()
{
for (int i = 0; i < next - 1; i++)
{
for (int j = i + 1; j > 0; j--)
{
if (list[j - 1] > list[j])
{
int temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
}
public void print()

{

for (int i = 0; i < next; i++)

{
Console.WriteLine(list[i]);
}
Console.WriteLine();
}
}
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

namespace UnorderedArrayListNamespace
{
   public class UnorderedArrayList
   {
       protected int[] list;
       protected int next;
      
       public UnorderedArrayList()
       {
           list = new int[100];
           next = 0;
       }
      
       public void insert(ref int item)
       {
           list[next] = item;
           next++;
       }
       public void remove(ref int item)
       {
           int i;
          
           if (next == 0)
           {
               Console.WriteLine("Empty List");
           }
          
           if (item.Equals(list[0]))
           {
               list[0] = list[--next];
           }
           else
           {   
               for (i = 0; i < next - 1; i++)
               {              
                   if (item.Equals(list[i]))
                   {
                       for (int j = i; j < next; j++)
                           list[j] = list[j + 1];
                       next--;
                       break;
                   }              
               }
          
               if (item.Equals(list[next - 1]) && i == next - 1)
                   next--;
           }
       }
      
       public void removeAll(ref int item)
       {
           if (next == 0) // empty list
           {
               Console.WriteLine("Empty List");
           }
           else
           {
               int i=0; // set i to 0
               // loop till the end of array
               while(i < next)
               {
                   // if ith item of array = item,
                   if(item.Equals(list[i]))
                   {
                       list[i] = list[next-1]; // copy the last item of array to ith index
                       next--; // decrement next
                   }else // increment i, i.e go to next array element
                       i = i + 1;
               }
           }
       }
      
       public int min()
       {
           int x = list[0];
           for (int i = 1; i < next; i++)          
           {
               if (x > list[i])
                   x = list[i];
           }
           return x;
       }
      
       public int max()
       {
           int x = list[0];
          
           for (int i = 1; i < next; i++)          
           {
               if (x < list[i])
                   x = list[i];
           }
           return x;
       }
      
       public void insertionSort()
       {
           for (int i = 0; i < next - 1; i++)
           {
               for (int j = i + 1; j > 0; j--)
               {
                   if (list[j - 1] > list[j])
                   {
                       int temp = list[j - 1];
                       list[j - 1] = list[j];
                       list[j] = temp;
                   }
               }
           }
       }
      
       public void print()
       {
      
           for (int i = 0; i < next; i++)          
           {
               Console.WriteLine(list[i]);
           }
           Console.WriteLine();
       }
   } // end class UnorderedArrayList
  
}   //end namespace UnorderedArrayListNamespace


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnorderedArrayListNamespace;

namespace Array
{
   public class Program
   {
       public static void Main(string[] args)
       {

           UnorderedArrayList u = new UnorderedArrayList();

           u.print();
           int var = 5;
           u.insert(ref var);
           var = 12;
           u.insert(ref var);
           var = 2;
           u.insert(ref var);
           var = 29;
           u.insert(ref var);
           var = 7;
           u.insert(ref var);
           var = 33;
           u.insert(ref var);
           var = 49;
           u.insert(ref var);
           var = 7;
           u.insert(ref var);
           var = 3;
           u.insert(ref var);
           Console.WriteLine("Starting");
           u.print();
           Console.WriteLine("Removed 29");
           var = 29;
           u.remove(ref var);
           u.print();
           Console.WriteLine("Removed 49");
           var = 49;
           u.remove(ref var);
           u.print();
           Console.WriteLine("All 7s Removed");
           var = 7;
           u.removeAll(ref var);
           u.print();
           Console.WriteLine("Maximum is: " + u.max());

           Console.WriteLine("Minimum is: " + u.min());

           u.insertionSort();

           Console.WriteLine("Sorted");

           u.print();
       }
   } //end class Program
} //end class Array

Output:

Starting 1: Removed 29 12 w Removed 49 wwwNNU

Removed 49 12 7 All 7s Removed 12 2 Maximum is: 33 Minimum is: 2 Sorted 33

Add a comment
Know the answer?
Add Answer to:
I cannot figure out why my removeAll for var = 7 is not working. using System;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • So I am working on an assignment where we make a rudimentary browser history with c#....

    So I am working on an assignment where we make a rudimentary browser history with c#. The requirement is that we need to use a linked list to do this. The issue that I am having is that when I want to go backwards in the history and print it takes from the beginning of the list and not the front. (Example is if I had a list with 1, 2, 3, 4 in it and went back It would...

  • C#, I am getting error placing them all in one file pls set them in the...

    C#, I am getting error placing them all in one file pls set them in the order that all 3 work in one file. Thanks //Program 1 using System; class MatrixLibrary { public static int[,] Matrix_Multi(int[,] a, int [,]b){ int r1= a.GetLength(0); int c1 = a.GetLength(1); int c2 = b.GetLength(1); int r2 = b.GetLength(0); if(r2 != c2){ Console.WriteLine("Matrices cannot be multiplied together.\n"); return null;    }else { int[,] c = new int[r1, c2]; for (int i = 0; i <...

  • I am currently using eclipse to write in java. A snapshot of the output would be...

    I am currently using eclipse to write in java. A snapshot of the output would be greatly appreciated to verify that the program is indeed working. Thanks in advance for both your time and effort. Here is the previous exercise code: /////////////////////////////////////////////////////Main /******************************************* * Week 5 lab - exercise 1 and exercise 2: * * ArrayList class with search algorithms * ********************************************/ import java.util.*; /** * Class to test sequential search, sorted search, and binary search algorithms * implemented in...

  • 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 namespace ConsoleApplication1 6...

    1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 namespace ConsoleApplication1 6 { class Program { 9 static void Main(string[] args) 10 { 11 test t = new test(10); Console.WriteLine(t. Getio); Console.WriteLine(t.J); 14 } 15 } 16 class test 17 { 18 18 private int i; 19 private int j; 20 public test(int a) 21 { 22 i = a; 23 j = a +5; public int Getio return i; } public int ) 29 30...

  • C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employee...

    C# Hey I am having trouble implementing additonal function to this assignment: Problem: Implement three IComparer classes on Employee - NameComparer, AgeComparer, and PayComparer - that allow Employees to be compared by the Name, Age, and Pay, respectively. Code: Employee.Core.cs using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Employees { partial class Employee { // Field data. private string empName; private int empID; private float currPay; private int empAge; private string empSSN = ""; private string empBene...

  • Im making a generic linked list. I cant figure out how to make an object of...

    Im making a generic linked list. I cant figure out how to make an object of my class from main. My 3 files are main.cpp dan.h dan.cpp The error is: 15 6 [Error] prototype for 'void ll<T>::insert()' does not match any in class 'll<T>' #include <iostream> #include <string> #include "dan.h" using namespace std; int main() { ll<int> work; work.insert(55);//THIS IS THE LINE THATS GIVING ME THE ERROR, Without this line it compiles and //runs. } #ifndef DAN_H #define DAN_H #include...

  • i need help fixing my code. here is the assignment: Make a class that contains the...

    i need help fixing my code. here is the assignment: Make a class that contains the following functions: isLong, isDouble, stringToLong, and stringToDouble. Each receives a string and returns the appropriate type (bool, bool, long, and double).During the rest of the semester you will paste this class into your programs and will no longer use any of the "standard" c# functions to convert strings to numbers. here is my code. it works for the long method but not the double:...

  • Greetings everyone I am having a little trouble with this one. I bolded my switch case...

    Greetings everyone I am having a little trouble with this one. I bolded my switch case which should work but its currently not. If a user selected course option 1 twice it just says you enrolled in course 1 and course 1. It should stop it by executing case -2. Need a new set of eyes. - Thanks using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleRegisterStudent { class Program { static void Main(string[] args) { (new...

  • C++ Time the sequential search and the binary search methods several times each for randomly generated...

    C++ Time the sequential search and the binary search methods several times each for randomly generated values, then record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Both the table and your conclusions should...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT