Solved

void bubble_sort(); -> general list

Posted on 2004-10-12
14
490 Views
Last Modified: 2008-01-09
// Do not understand these errors listed below after coding of main() {}.

// main.cpp

#include <iostream>
#include "list.h"
#include "util.h"

//const int MAXSIZE = 100;
#define MAXSIZE 100

void main()
{
   int data;
   int i;
   List<int> list_1;
   list_1.insert(0,1);
   list_1.insert(1,2);
   list_1.insert(2,3);
   list_1.insert(3,4);
   list_1.insert(4,5);
   list_1.retrieve(1, data);
   
   cout << data << endl;

   if(list_1.remove(9) == success)
    cout << "success";
   else
    cout << "range_error\n";

   list_1.remove(0);
   list_1.remove(2);
   list_1.remove(5);
   list_1.insert(2,2);
   list_1.insert(7,7);

   cout << "1st List: ";
   list_1.display();

   List<int> list_2(list_1);
   list_2.insert(2,2);

   cout << "2nd List: ";
   list_2.display();

   cout << "1st List: ";
   list_1.display();
   List<int> list_3 = list_1;
   cout << "3rd List: ";
   list_3.display();
   cout << endl;
   
void bubble_sort(List<int> &list_2);
 
  for(int i = 1; i <= MAXSIZE - 1; i++)
    for(int j = 0; j < MAXSIZE; j++)
      if(list<int>[j] = list[j+1])
      list i;
      i.insert(1);
      list j;
      j = i;
      i.delete(0);

}

generated output:

[edeloss2@pegasus hw4]$ make
cxx -c -L/usr/lib/cmplrs/cxx -DPOSIX_4D9  -gall main.cpp
cxx: Error: main.cpp, line 56: identifier "list" is undefined
      if(list<int>[j] = list[j+1])
---------^
cxx: Error: main.cpp, line 56: type name is not allowed
      if(list<int>[j] = list[j+1])
--------------^
cxx: Error: main.cpp, line 56: expected an expression
      if(list<int>[j] = list[j+1])
------------------^
cxx: Error: main.cpp, line 57: expected a ";"
        list i;
-------------^
cxx: Error: main.cpp, line 58: left operand of the "." operator must have a
          class type
        i.insert(1);
---------^
cxx: Error: main.cpp, line 59: identifier "list" is undefined
        list j;
--------^
cxx: Error: main.cpp, line 61: expected a member name
        i.delete(0);
----------^
cxx: Error: main.cpp, line 61: left operand of the "." operator must have a
          class type
        i.delete(0);
---------^
cxx: Info: 8 errors detected in the compilation of "main.cpp".
make: *** [main.o] Error 1





0
Comment
Question by:edelossantos
14 Comments
 

Author Comment

by:edelossantos
Comment Utility
According to my professor, the bool implementation does not make any sense in relation to main.

Can I get a hint in the rite direction?
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 375 total points
Comment Utility
You have something called List defined, but nothing called list with a lower case 'l'.

Also be careful in the following:

--------8<--------
  for(int i = 1; i <= MAXSIZE - 1; i++)
    for(int j = 0; j < MAXSIZE; j++)
      if(list<int>[j] = list[j+1])
     list i;
     i.insert(1);
     list j;
     j = i;
     i.delete(0);
--------8<--------

Do you want to put braces around a block of code? Do you want to have an integer called 'i' and a list called 'i'?
0
 

Author Comment

by:edelossantos
Comment Utility
for(int i = 1; i <= MAXSIZE - 1; i++) {
    for(int j = 0; j < MAXSIZE; j++) {
       if(List[j] = List[j+1])
       List;
       insert(1);
       List j;
       j = i;
       delete(0);

    }
   }

Is this closer to the coreect format?...in looking at the main I am guessing that I am going to have implement insert and remove v.s. int i.
0
 

Author Comment

by:edelossantos
Comment Utility
rstaveley

sort(list_2);

   cout << "2nd List Sorted: ";
   list_2.display();
   cout << endl;  

}

This is part of the sort code that needs to be coded.  Am I close or way off base?
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 375 total points
Comment Utility
Are you allowed to use std::vector<> in this assignment or have you been asked to work with something home-grown?

I'm guessing that you've written a generic container called List, which you are using to contain ints.

If so, consider the perfectly valid looking declaration:

    List<int> list_1;

In the declaration, List<int> is the variable type. It is a List of ints. The variable name is list_1.

Once you have made this declaration, you should be working with list_1 to manipulate the list.

Let's look at your correction:

--------8<--------
 for(int i = 1; i <= MAXSIZE - 1; i++) {
    for(int j = 0; j < MAXSIZE; j++) {
       if(List[j] = List[j+1])  /* Do you want to test list_1[j] == list_1[j+1] ?? */
       List;  /* What are you trying to do here? */
       insert(1); /* Do you have a global function called insert? I suspect you want to insert something in list_1, but that's going to be out of scope in a global function */
       List j; /* If you want to declare a new list, don't you want List<int> j... but j is your inner loop int variable name... so j is not a good name for a new variable */
       j = i; /* Is this supposed to assign one of the loop int variable to the other?? */
       delete(0); /* Do you have a global function called delete? I suspect you want to delete something in list_1, but that's going to be out of scope in a global function */

    }
   }
--------8<--------

It sounds to me like you are trying to run before you can walk. You need to get more comfortable with C++ basics before you get into this advanced stuff.
0
 

Author Comment

by:edelossantos
Comment Utility
std::vector<> is acceptable.
0
 

Author Comment

by:edelossantos
Comment Utility
>>It sounds to me like you are trying to run before you can walk. You need to get more comfortable with C++ basics before you get into this advanced stuff.

You are correct, unfortunately my foundation is not that strong...; however,  I like computer science as well as math..so therefore I am willing to hang in there.  I've repeated a couple of classes and came out with the same results; therefore, I am sort of weary of retaking classes.  I have purchased additional books, but even with that at times I am still unable to find the information that I need.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Assisted Solution

by:pankajtiwary
pankajtiwary earned 125 total points
Comment Utility
Do an std::list in place of normal list. Or you can use a using declaration. Just say using namespace std ; before main().

Also please start writing int main() instead of void main(). The standard says main returns an int and not void. Doing void invokes an undefined behavious and your code is not reliable.
0
 
LVL 17

Assisted Solution

by:rstaveley
rstaveley earned 375 total points
Comment Utility
Well... all we can really see from this is that you want to work with a container of ints.

The two most obvious choices for a container are a vector or a linked list. When you are working with a primitive object like an int, you don't generally use lists, because they are so inexpensive to move around, which makes all of the cleverness of a linked list unnecessary baggage. If this was a commercial project, I'd recommend that you used a vector not a list.

But... this is an academic exercise, which means that you may well have been asked to work with a list of ints for the sake of learning how to work with lists.

You've got something in list.h and you appear to be inserting elements into the middle of the list, which suggests that you want to work with linked lists. If you are under the impression that "list.h" is a system header, you are wrong. For the standard library linked list std::list<> you should #include <list>. If you've been asked to implement your own linked list as an academic exercise, we'd need to see the contents of list.h to be able to help you with it.

However, you appear to be attempting to index elements from the list, using the [] operator. This is something that you can do with C-arrays or a vector (e.g. the standard library's std::vector<> ). std::vector<> has random access, and std::list<> does not.

So... what's the assignment?
0
 
LVL 17

Accepted Solution

by:
rstaveley earned 375 total points
Comment Utility
Classes may not be the best way for you to sure up your C++ foundations. If I were you I'd be investigating on-line tutorials at this point. IMHO lectures are a facility to transcribe lecturers' notes into students' notes with the maximum number of transcription errors and minimum amount of understanding.

Don't know if this is helpful to you, but you might find the following code helpful to learn how to work with the standard library list implementation (std::list<>).

std::list<> lends itself to being sorted easily and has a member function called sort to do so, unlike std::vector<> which requires you to use the sort algorithm from #include <algorithm>. The sort function you get in #include <algorithm> can be used with std::list<>, but may be less efficient than the member function for std::list<>. Similarly std::list<> has a remove member function, which you can see below. I'm not sure why they decided not to implement find as a member function; you need to use the #include <algorithm> find, which I've used to find an insert iterator.

Study the code below. Code is often easier to understand than words. I hope this helps.

--------8<--------
#include <iostream>             // std::cout
#include <list>                 // Standard library linked list (std::list<>)
#include <iterator>             // Used by the display function (std::ostream_iterator)
#include <algorithm>            // Used by display function (std::copy), and std::find
#include <string>               // std::string - not using char*

typedef std::list<int> IntList; // Declare a list of ints as a type
typedef IntList::iterator IntListItr; // Declare an iterator into a list of ints as a type

// Display the ints in the given list
void display(const std::string& prefix,const IntList& l)
{
        std::cout << prefix << ": ";
        copy(l.begin(),l.end(),std::ostream_iterator<int>(std::cout," "));
        std::cout << '\n';
}

// Convenience macro to display the list, with its variable name as the prefix
#define DISPLAY(l) display(#l,l)

int main()
{
        std::cout << "List demo using std::list<int>\n";

        IntList list1;          // We have a list of ints called list1
        std::cout << "\nAdding 1,3,2 to the end of list1\n";
        list1.push_back(1);     // Put values 1,3,2 into the list
        list1.push_back(3);
        list1.push_back(2);
        DISPLAY(list1);

        // Inserting into the list at the beginning and end using iterators
        std::cout << "\nInserting -1 at the beginning of list1 and -3 at the end\n";
        list1.insert(list1.begin(),-1);
        list1.insert(list1.end(),-3);
        DISPLAY(list1);

        // Inserting into the list before the 2
        std::cout << "\nInserting 22 before the 2\n";
        IntListItr itr = find(list1.begin(),list1.end(),2);
        if (itr == list1.end())
                std::cout << "Error: Unable to find 2 in list1\n";
        else
                list1.insert(itr,22);
        DISPLAY(list1);

        std::cout << "\nAdding 1,3 to the end of list1 (duplicates)\n";
        list1.push_back(1);
        list1.push_back(3);
        DISPLAY(list1);

        // Removing the first 1
        std::cout << "\nRemoving first 1 using find and erase\n";
        /*IntListItr*/ itr = find(list1.begin(),list1.end(),1);
        if (itr == list1.end())
                std::cout << "Error: Unable to find 1 in list1\n";
        else
                list1.erase(itr);
        DISPLAY(list1);

        // Removing all 3s from the list
        std::cout << "\nRemoving all 3s using remove\n";
        list1.remove(3);
        DISPLAY(list1);

        std::cout << "\nGetting a copy of list1 as list2 and sorting it\n";
        IntList list2 = list1;  // Get a copy of the list
        list2.sort();           // Use the std::list<> member function to sort
        DISPLAY(list2);         // Display the copy of the list
        DISPLAY(list1);         // Original list should be unsorted
}
--------8<--------
0
 
LVL 17

Expert Comment

by:rstaveley
Comment Utility
Or has your professor asked you to write your own bubble sort algorithm? (I've just noticed your quetion at http:/Q_21163138.html )
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
Del, you have three questions to the same problem. Look at

  http:Q_21163138.html

Regards, Alex

P.S.

Don't give me points for this comment.
0
 

Author Comment

by:edelossantos
Comment Utility
Professors,

I apologize for the mutliple postings of the same question...I have been asked to post only once, but sometimes with being under the gun...at times if not at most...I act hastily;however, all of the information given has helped immensly.
Therefore, I will submit points to all.  Regards and a trillion thanks.  Del
0
 

Author Comment

by:edelossantos
Comment Utility
rstaveley

>>Or has your professor asked you to write your own bubble sort algorithm?

Professor gave partial code...the rest needed to be coded.  Del
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now