C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 1st, 2003, 11:00 PM
supaben34 supaben34 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Tallahassee
Posts: 55 supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 8
My list class

Hi guys,
Let me start off by saying that this is a project from school. I've been working on this awhile now and I still cannot get it to work. Please tell me what I am doing wrong. I am going to post list.h and list.cpp in this page. Thank you for your concern.
Code:
// Program Description: This is a header file for the list class

#ifndef LIST_H
#define LIST_H

#include <iostream.h>

const int SIZE = 20;

class List{
 public:
  List();
  void Show();
  bool Insert(int value);
  bool Insert(int value, int pos);
  bool Delete(int pos);
  int Size();
  int Sum();
  float Average();
  int Odds();
  int HowMany(char value);
  int Between(int low,int high);
  int WhereIs(int value);
  void Clear();
 private:
  int list_array[SIZE];// the container -- an array of 20 integers
  int element;// a pointer that keeps track of the position in array
};

#endif


Code:
/*-------------------------------------list.cpp------------------------------------------*/

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

List::List()
  // constructor for list object - has to create the list object and print out message that
  // object has been instantiated
{
  element = 0;
  cout << "Creating List Object\n";
}

void List::Show()
  // if list is empty, output message
  // else show elements in list
{
  if (element <= 0)
    cout << "List is empty\n";
  else if (element >= SIZE)
    cout << "List is full\n";
  else{
    for (int i = 0; i < element; i++)
      cout << list_array[i] << " ";
    cout << "\n";
  }
}
bool List::Insert(int value)
  // Inserts a number at the end of the list(if there is enough room)
{
  if (element < SIZE){
    list_array[element] = value;
    element = element++;
    return true;
  }
  else{
    cout << "*** Invalid list position number\n";
    return false;
  }
}
bool List::Insert(int pos, int value)
  // inserts a number in the list, possibly in the middle(if room, and if valid position given)
{
  if (pos <= (element+1)){
    // In order to insert another value, we need atleast one more free space
    if (((element+1) < SIZE) && (pos > 0)){

      // starting at element, begin  moving every element one space to the right
      for(int i = (element+1); i <= pos; i--)
        list_array[i + 1] = list_array[i];

      // at pos, insert new value;
      list_array[pos] = value;

      // increment element
      element = element + 1;
      return true;
    }
  }
  else {
    cout << "*** Invalid position number\n";
    return false;
  }
}
bool List::Delete(int pos)
  // delete a value from the list(given a position)
{

  if ((element < 0) && (element > SIZE)){
    cout << "*** Invalid list position number";
    return false;
  }

  else{
    // shift value at pos + 1 and so on to the left
    for (int i = element; i > pos; i--)
      list_array[i] = list_array[i + 1];

    // decrease element by one
    element = element - 1;

    return true;
  }
}
int List::Size()
  // returns the size of list
{
  return element;
}

int List::Sum()
  // returns the sum of the values in the list
{
  int sum = 0;
  for (int i = 0; i < element; i++)
    sum += list_array[ i ];
  return sum;
}

float List::Average()
  // returns the average of the values in the list
{
  float sum = Sum();
  float size = Size();
  float average = sum/size;
  return average;
}
int List::Odds()
  // counts up and returns how many odd numbers in the list
{
  int odd_count = 0;
  for (int i = 0; i <= element; i++){
    if ((list_array[i] % 2) == 1){
      odd_count++;
    }
  }
  return odd_count;
}

int List::HowMany(char value)
  // counts up and returns how many occurences of a given value(should work for any parameter value)
{
  int how_many_counter = 0;
  for (int i=0; i < element; i++){
    if (list_array[i] == value)
      how_many_counter++;
  }
    return how_many_counter;
}
int List::Between(int low,int high){
  int between = 0;
  int value;
  for(int i = 0; i < element; i++){
    value = list_array[i];
    if((value >= low) && (value <= high))
      between++;
  }
  return between;
}

int List::WhereIs(int value)
  // returns the position of the first occurence of a given value(or returns 0, if not found)
{

  int num;   // This variable tells us what number is in array_list[i]
  // This is just a counter to capture the position we are on
  int pos_counter = 0;

  for (int i = 0; i <= element; i++){
    num = list_array[i];
    if (num == value)
      pos_counter = (i + 1);  // capture the array number + 1
  }
  return pos_counter;
}

void List::Clear()
  // resets the list object to represent an empty list
{
  element = 0;
}

I just need help on the Insert(int pos, int value) in particular. The Delete() does not work either but I'll get to that later. I just added the rest of the code for the sake of completeness. Thank you so much.

Reply With Quote
  #2  
Old July 1st, 2003, 11:48 PM
infamous41md's Avatar
infamous41md infamous41md is offline
not a fan of fascism (n00b)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Feb 2003
Location: ct
Posts: 2,756 infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level)infamous41md User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 2 Days 11 h 4 m 29 sec
Reputation Power: 26
i made comments:
Code:
bool List::Insert(int pos, int value)
  // inserts a number in the list, possibly in the middle(if room, and if valid position given)
{
  if (pos <= (element+1)){      //here u should check if the position is one less than the SIZE constant instead
    // In order to insert another value, we need atleast one more free space
    if (((element+1) < SIZE) && (pos > 0)){     //should be (element + 1) <= SIZE , since size is the max number of elements

      // starting at element, begin  moving every element one space to the right
      for(int i = (element+1); i <= pos; i--) //your starting PAST the last index with (i = (element + 1)), should (element - 1)
        list_array[i + 1] = list_array[i];      // and i >= since your moving backwards

      // at pos, insert new value;
      list_array[pos] = value;

      // increment element
      element = element + 1;
      return true;
    }
  }
  else {
    cout << "*** Invalid position number\n";
    return false;
  }
}



have a look at this, i thinks its mostly correct
Code:
bool List::Insert(int pos, int value)
{       // if we have space and the position is positive
        if((element < (SIZE - 1))&&(pos >= 0)&&(pos <=19))
        {
                //start from furthest element to right and shift each element one to the right
                for(int x = (elem - 1); x >= pos; x--)
                {
                        list_array[x + 1] = list_array[x];
                }
                //now replace the desired position
                list_array[pos] = value;
                //increment list size
                element++;
                return true;
        }
        else    // error
        {
                cerr << "invalid position\n";
                return false;
        }
}

Last edited by infamous41md : July 1st, 2003 at 11:55 PM.

Reply With Quote
  #3  
Old July 2nd, 2003, 09:08 AM
supaben34 supaben34 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2002
Location: Tallahassee
Posts: 55 supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level)supaben34 User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 h 57 m 51 sec
Reputation Power: 8
I think you're real close there infamous41md but when you insert the characters the first element is 1 not 0. For example
Code:
1 2 3 4 5 10 25 40 50 65
// the first number, '1' is in position 1, not 0


My code still does not work.

Last edited by supaben34 : July 2nd, 2003 at 09:13 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > My list class


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway