GithubHelp home page GithubHelp logo

algorithms-and-data-base-project-1.'s Introduction

Algorithms and data structures - project 1.

The assignment is to write a code that sums up numbers after the user has entered a group of numbers.

//Unsorted List data structure is represented in memory by a dynamic array //and implemented as the C++ class List. #include

using namespace std; //****************************************************************** class List { public: List(int = 10); //constructor with the default value of its parameter ~List(); //destructor bool isFull() const; bool isEmpty() const; int length() const; //The number of list items is returned. void makeEmpty(); int searchUnsorted(int) const; //If found, index of the 1st occurrence //of item is returned, otherwise -1. void addItem(int); //It inserts an item at the list end. int deleteItem(int); //It deletes and returns an item. void printList() const; void sumitems() const;

protected: int max; //array size int last; //index of the last list item int* array; //pointer to the first element of a dynamic array

};//List //*************************************************************** // class List methods //************************************************************** List::List(int n) //n is the expected maximum number of items in a list. { max = n; //Array size. last = -1; array = new int[max]; //Dynamic array is created. }//constructor //************************************************************** List::~List() { last = -1; delete[]array; }//destructor //************************************************************** void List::makeEmpty() { last = -1; }// makeEmpty //************************************************************** bool List::isEmpty() const { return (last == -1); }// isEmpty //************************************************************** bool List::isFull() const { return (last == max - 1); }// isFull //************************************************************** int List::length() const { return (last + 1); }// length //************************************************************** int List::searchUnsorted(int item) const { for (int i = 0; i <= last; i++) if (array[i] == item) return i; return -1; }//searchUnsorted //************************************************************** void List::addItem(int item) { if (isFull()) cout << "Memory overflow: item cannot be added to list\n."; else array[++last] = item; //item is added at the end of unsorted list. }// addItem //************************************************************** int List::deleteItem(int item) { if (!isEmpty()) { int k = searchUnsorted(item); if (k == -1) cout << "\nInvalid argument:Item was NOT FOUND in list.\n"; else { int dataReturned = array[k]; //The last item overwrites the deleted item. array[k] = array[last--]; return dataReturned; } } else //list is empty cout << "\nList underflow: item cannot be deleted.\n"; return -10000; //to signal that deleting failed if NOT FOUND or EMPTY }//deleteItem //************************************************************ void List::printList() const { cout << "\nList contents:\n"; if (last == -1) cout << "\nPrinting failed: list is empty\n"; else { for (int i = 0; i <= last; i++) cout << array[i] << ", "; cout << endl; }//else }//printList //************************************************************* //*************************************************************

// sum of items in unsorted list void List::sumitems() const

{ int sum = 0;

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

{


    sum = sum + array[i];



}

cout << "\nsum of array elements :" << sum<<endl;

}

int main()

{ cout << "\nExperimenting with unsorted list of integer numbers:\n\n"; List B(5); //An empty list B is created. It can store up to 15 items. int a1; for (int i = 1; i <= 17; i++) { cout << "Enter int number, -7 to stop ==> "; cin >> a1; if (a1 != -7) B.addItem(a1); else break; //go outside the loop block

}

B.printList();
B.sumitems();

}//main

algorithms-and-data-base-project-1.'s People

Contributors

emmanuel092 avatar nateous avatar

Watchers

James Cloos avatar  avatar

Forkers

nateous

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.