9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 1/13
SlideShare Explore Search You
UploadLoginSignup
Search
HomeTechnologyEducationMore Topics
For UploadersCollect Leads
Get Started
Tips & Tricks
Tools
For Business
Search
Data structures / C++ Programexamples18,319
ShareLikeDownload
Related Searches
Share Photos
Free PowerpointTemplates
DreamweaverTemplates
Brochure Templates
Web Templates
Presentation Software
Share Videos
Powerpoint Templates
Online Webinars
PowerpointBackgrounds
?
Related Searchby Magical Find
Magical Find Ad
s
Magical Find Ad
s
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 2/13
Kevin III, IT Specialist at AppleFollow0 21 1 1
Published on Nov 10, 2012
Published in: Education
0 Comments5 LikesStatisticsNotes
Full NameComment goes here.12 hours ago Delete Reply Spam BlockAre you sure you want to Yes NoYour message goes here
Share your thoughts...Post
Be the first to comment
WMHan Farhan at University of Malaya11 months ago
prabhakar rathod at National Institute of Technology Warangal (NITW)1 year ago
Ugyen Thinley at Student1 year ago
if $ticker>lng_id==1 else"???????????? ??????????"|translate:10/ifLoading...
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 3/13
abdurrahman adem1 year ago
Shraddha Bhatt2 years ago
No DownloadsViewsTotal Views18,319On Slideshare0From Embeds0Number of Embeds0ActionsShares23Downloads495Comments0Likes5Embeds 0No embeds
No notes for slide
Data structures / C++ Program examples
1. 1. IT 211DATA STRUCTURES MIDTERM REQUIREMENT2. 2. IT 211 DATA STRUCTURES MIDTERM REQUIREMENT 1. Write a
program to enter the name and votes of 5 candidates. The program shoulddisplay each candidate’s name, votes and percentage of votes. The programshould also declare the winner. #include <string> #include<iostream>using namespace std; class ElectionResults public: string name; int votes;; int main() ElectionResults res[5]; int voteTotal = 0; int winner = 0; intbyVotes = 0; float percent = 0.0; for(int i = 0; i < 5; i++) cout << "EnterLast Name of Candidate "<<i+1<<" : "; cin >> res[i].name; cout<<"Enterthe Votes Received for Candidate "<<i+1<< ": "; cin >> res[i].votes;cout<<""<<endl; voteTotal+= res[i].votes; if( res[i].votes > byVotes) winner = i; Kevin Dulay IIILleif Christian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
3. 3. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTcout<<"Name of candidatetVotes receivedtPercentage"<<endl; for(int i =
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 4/13
0; i < 5; i++) cout<<res[i].name<<"ttt"<< res[i].votes <<"ttt"<<(res[i].votes*100)/voteTotal << "%." <<endl; cout<<"nTotal Votes: "<<voteTotal<<endl; cout << "The Winner is:" << res[winner].name<<endl;cin >> voteTotal; Kevin Dulay III Lleif Christian BinasoyHazel JoyPaguyo Joanna Katrin Corpuz
4. 4. IT 211 DATA STRUCTURES MIDTERM REQUIREMENT 2.STRINGS a. Write a program to accept and display the number of letters,words and sentences of a text input. #include <iostream> #include <string>#include <cctype> using namespace std; int main() string userInput; charmyChar; int words = 1; int sentences = 0; int paragraphs = 1; cout <<"Enter some text: "; getline (cin, userInput); for (int i = 0; i<int(userInput.length()); i++) myChar = userInput.at(i); /*if (userInput[i]== isascii(NUL)) words ; paragraphs ; */ if (userInput[i] == )words++; if (userInput[i] == .) sentences++; if (userInput[i] == n &&userInput[i] == t) paragraphs++; cout << "words: " << words << endl;cout << "sentences: " << sentences << endl; cout << "paragraphs: " <<paragraphs <<endl; int p; cin >> p; return 0; Kevin Dulay III LleifChristian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
5. 5. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTb. Write aprogram to determine if a word is palindrome #include<iostream.h>#include<string.h> int main() char str[25],str1[25]; cout<<"Enter a string:"; gets(str); strcpy(str1,str); strrev(str); if(strcmp(str,str1)!=0) cout<<"string is not palindrome"; else cout<<"string is a palindrome"; cout<<endl; cin.get(); return 0; Kevin Dulay III Lleif ChristianBinasoyHazel Joy Paguyo Joanna Katrin Corpuz
6. 6. IT 211 DATA STRUCTURES MIDTERM REQUIREMENT 3.RECURSION Write a program to convert a binary number to its decimalequivalent using a recursive function. //BINARY TO DECIMAL #include<iostream> #include <math.h> using namespace std; int sum = 0; usingnamespace std; int BinaryToDecimal (int BinaryNumber, int weight); intmain() int bitWeight; int binaryNum; bitWeight = 0;cout<<"======CONVERTING BINARY TO DECIMAL======="<<endl; cout<<"Enter the Binary Number: "; cin>>binaryNum; cout<<"n";int sum = BinaryToDecimal (binaryNum, bitWeight); cout<<"The DecimalEquivalent of inputed Binary is: "<<sum<<endl; system("PAUSE"); return0; int BinaryToDecimal (int BinaryNumber, int weight) int bit; if(BinaryNumber>0) bit = BinaryNumber % 10; sum = sum + bit * pow (2,weight); BinaryNumber = BinaryNumber /10; weight++; BinaryToDecimal(BinaryNumber, weight); return sum; Kevin Dulay III LleifChristian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
7. 7. IT 211 DATA STRUCTURES MIDTERM REQUIREMENT//DECIMAL TO BINARY #include <iostream> using namespace std; voidbinary(int); int main(void) int number; cout << "Please enter a positiveinteger: "; cin >> number; if (number < 0) cout << "That is not a positiveinteger.n"; else cout << number << " converted to binary is: ";binary(number); cout << endl; void binary(int number) int remainder;if(number <= 1) cout << number; return; remainder = number%2;binary(number >> 1); cout << remainder; cin.get(); Kevin Dulay III LleifChristian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
8. 8. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTLECTURE
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 5/13
REQUIREMENTSResearch on the definitions, C++ implementation, andprogram examples of thefollowing: Stack Lists Queues Pointers1. StackLIFO stackStacks are a type of container adaptor,specifically designed to operate in a LIFO context(lastin firstout), whereelements are inserted and extracted only from the end ofthecontainer.stacks are implemented as containers adaptors, which areclasses that use an encapsulatedobject of a specific container class as itsunderlying container, providing a specific set ofmember functions to accessits elements. Elements are pushed/popped from the "back" ofthe specificcontainer, which is known as the top of the stack.The underlying containermay be any of the standard container class templates or someotherspecifically designed container class. The only requirement is that itsupports thefollowing operations: back() push_back() pop_back()Therefore, the standard container class templates vector, dequeand list can be used. Bydefault, if no container class is specified for aparticular stack class, the standard containerclass template deque is used.Intheir implementation in the C++ Standard Template Library, stacks taketwo templateparameters: template < class T, class Container = deque<T> >class stack;Kevin Dulay III Lleif Christian BinasoyHazel Joy PaguyoJoanna Katrin Corpuz
9. 9. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTWhere thetemplate parameters have the following meanings: T: Type of theelements. Container: Type of the underlying container object used tostore and access the elements.Kevin Dulay III Lleif ChristianBinasoyHazel Joy Paguyo Joanna Katrin Corpuz
10. 10. IT 211 DATA STRUCTURES MIDTERM REQUIREMENT2.LISTSLists are a kind of sequence container. As such, their elements areordered following a linearsequence.List containers are implemented asdoublylinked lists; Doubly linked lists can store each ofthe elements theycontain in different and unrelated storage locations. The ordering is keptbythe association to each element of a link to the element preceding it and alink to theelement following it.This provides the following advantages tolist containers: Efficient insertion and removal of elements before anyother specific element in the container (constant time). Efficient movingelements and block of elements within the container or even betweendifferent containers (constant time). Iterating over the elements inforward or reverse order (linear time).Compared to other base standardsequence containers (vectors and deques), lists performgenerally better ininserting, extracting and moving elements in any position withinthecontainer for which we already have an iterator, and therefore also inalgorithms that makeintensive use of these, like sorting algorithms.Themain drawback of lists compared to these other sequence containers is thatthey lackdirect access to the elements by their position; For example, toaccess the sixth element ina list one has to iterate from a known position(like the beginning or the end) to thatposition, which takes linear time inthe distance between these. They also consume someextra memory to keepthe linking information associated to each element (which may beanimportant factor for large lists of smallsized elements).Storage ishandled automatically by the list object, allowing it to be expandedandcontracted automatically as needed.In their implementation in the C++
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 6/13
Standard Template Library lists take two templateparameters: template <class T, class Allocator = allocator<T> > class list;Kevin Dulay III LleifChristian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
11. 11. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTWherethe template parameters have the following meanings: T: Type of theelements. Allocator: Type of the allocator object used to define thestorage allocation model. By default, theallocator class template for type Tis used, which defines the simplest memory allocation model and is valueindependent.In the reference for the list member functions, these samenames are assumed for thetemplate parameters.Kevin Dulay III LleifChristian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
12. 12. IT 211 DATA STRUCTURES MIDTERMREQUIREMENT3.QueuesFIFO queuequeues are a type of containeradaptor, specifically designed to operate in a FIFO context(firstin firstout), where elements are inserted into one end of the container andextractedfrom the other.queues are implemented as containers adaptors,which are classes that use anencapsulated object of a specific containerclass as its underlying container, providing aspecific set of memberfunctions to access its elements. Elements are pushed intothe "back" of thespecific container and popped from its "front".The underlying containermay be one of the standard container class template or someotherspecifically designed container class. The only requirement is that itsupports thefollowing operations: front() back() push_back() pop_front()Therefore, the standard container class templates deque and listcan be used. By default, ifno container class is specified for a particularqueue class, the standard container classtemplate deque is used.KevinDulay III Lleif Christian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
13. 13. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTIn theirimplementation in the C++ Standard Template Library, queues take twotemplateparameters: template < class T, class Container = deque<T> >class queue;Kevin Dulay III Lleif Christian BinasoyHazel Joy PaguyoJoanna Katrin Corpuz
14. 14. IT 211 DATA STRUCTURES MIDTERMREQUIREMENT4.PointersA pointer is a variable that is used to store amemory address. The address is thelocation of the variable in the memory.Pointers help in allocating memorydynamically. Pointers improveexecution time and saves space. Pointer points to aparticular data type. Thegeneral form of declaring pointer is: type *variable_name;type is the basetype of the pointer and variable_name is the name of the variable ofthepointer. For example, int *x;x is the variable name and it is the pointer oftype integer.Pointer OperatorsThere are two important pointer operatorssuch as ‘*’ and ‘&’. The ‘&’ is a unaryoperator . The unary operatorreturns the address of the memory where a variable islocated. For example,int x*; int c; x=&c;Kevin Dulay III Lleif Christian BinasoyHazel JoyPaguyo Joanna Katrin Corpuz
15. 15. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTvariable xis the pointer of the type integer and it points to location of the variablec.When the statement x=&c;is executed, ‘&’ operator returns the memoryaddress of the variable c and as aresult x will point to the memory locationof variable c.The ‘*’ operator is called the indirection operator . It returns
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 7/13
the contents of thememory location pointed to. The indirection operator isalso called deferenceoperator. For example, int x*; int c=100; int p; x=&c;p=*x;variable x is the pointer of integer type. It points to the address of thelocation of thevariable c. The pointer x will contain the contents of thememory location of variablec. It will contain value 100. When statementp=*x;is executed, ‘*’ operator returns the content of the pointer x andvariable p willcontain value 100 as the pointer x contain value 100 at itsmemory location. Here isa program which illustrates the working ofpointers.Kevin Dulay III Lleif Christian BinasoyHazel Joy Paguyo JoannaKatrin Corpuz
16. 16. IT 211 DATA STRUCTURES MIDTERMREQUIREMENT#include<iostream>using namespace std;int main () int*x; int c=200; int p; x=&c; p=*x; cout << " The address of the memorylocation of x : " << x << endl; cout << " The contents of the pointer x : "<< *x << endl; cout << " The contents of the variable p : " << p << endl;return(0);The result of the program is:Kevin Dulay III Lleif ChristianBinasoyHazel Joy Paguyo Joanna Katrin Corpuz
17. 17. IT 211 DATA STRUCTURES MIDTERM REQUIREMENTKevinDulay III Lleif Christian BinasoyHazel Joy Paguyo Joanna Katrin Corpuz
RecommendedMore from this author
Foundations of Programming: Data Structures
Foundations of Programming: Databases
Foundations of Programming: Fundamentals
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 8/13
Data structure and algorithms in c++Karmjeet Chahal1,328
Data structure through c++ By Yashvant Kanetkar book reviews atDecidebuddy.comDecidebuddy3,243
C++ plus data structuresLong Long6,017
Data structures and program design in c++ robert l. krusesoskmn21,488
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 9/13
C++ Sample Codes (Data Structure)Jenelyn Pañoso1,739
Let us c++ yashwant kanetkar (1)student10,797
Data structures projectsachinrajsachin18,847
Data Structure and Algorithm in C/C++Ngeam Soly9,331
Ds lab handoutsayeshasaifbhatti2,701
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 10/13
Problem solving and program design in c 7eMohammed Jawad Ishaque47,853
Principles of data structures using c and c++ december 2008Edi sa puso mo :">16,158
DATA STRUCTURESbca201026,932
Programming with c++munifkhanfer7,365
Structure In C++Abdul Waqar1,228
Student Data Base Using C/C++ Final ProjectCH Nawaz16,497
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 11/13
Ang Pananaliksik at Mananliksik Filipino 002Kevin III2,236
Employment of ict graduates in region 2Kevin III589
History of ZyngaKevin III1,347
Confirmation ppt reportKevin III3,104
Organizational communication english reporrtKevin III2,003
Introduction to AptanaKevin III1,895
ENGLISHEnglish
9/6/2015 Data structures / C++ Program examples
http://www.slideshare.net/kevinrdulay/datastructurescprogramexamples 12/13
FrançaisEspañolPortuguês (Brasil)Deutsch
EnglishEspanolPortuguesFrançaisDeutsche
AboutDev & APIBlogTermsPrivacyCopyrightSupport
LinkedIn Corporation © 2015
×
Share Clipboard
×
Enter email addresses
From Add a message
SendEmail sent successfully..
FacebookTwitterLinkedInGoogle+
Link
Top Related