Forum Replies Created
- AuthorPosts
- GWILouisaxwzklaParticipant
Don’t know what type of sort you need to use but heres one with bubble sort:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117/***************************************************************** File Name : c:programstempCG.cpp* Date : December,29,2009* Comments : new project* Compiler/Assembler : Visual C++ 6.0* Modifications :****** Program Shell Generated At: 11:37:28 a.m.=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/#include < iostream >//#include < string.h >//#include < conio.h >//#include < math.h >//#include < iomanip >//#include < ctype.h >using namespace std;void bubbleSort ( int * array , int numberItems );//main function ******************************const int MAX_NUMBERS = 20;int main ( ){int input;int numberOfInputItems;int numberCount = 0;int numbers [ MAX_NUMBERS ];cout << "Enter number of items to input: ";cin >> numberOfInputItems;int i = 0;while ( i < numberOfInputItems ){cout << "enter a number: ";cin >> numbers [ i ] ;if ( numbers [ i ] == 1 || numbers [ i ] == 4 || numbers [ i ] == 5 || numbers [ i ] == 12 ||numbers [ i ] || numbers [ i ] == 3 )numberCount ++;i ++;}cout << endl;cout << "The count was " << numberCount << endl << endl;bubbleSort ( numbers , i );int j = 0;while ( j < i ){cout << numbers [ j ] << " ";j ++;}}/******************************* FUNCTION DEFINITION ******************************Name : bubbleSortParameters :array a(n) int * ,numberItems a(n) intReturns: Void typeComments:++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/void bubbleSort ( int * array , int numberItems ){int endSortedArray = numberItems - 1;int lastSwapIndex;int temp;while ( endSortedArray > 0 ) //while not at the end of the unsorted array{lastSwapIndex = 0; //save index of the last item swappedint i = 0; //start at the beggining of the unsorted arraywhile ( i < endSortedArray ) //while not in the sorted items{if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up{//swap array [ i ] and array [ i + 1 ]temp = array [ i ];array [ i ] = array [ i + 1 ];array [ i + 1 ] = temp;lastSwapIndex = i;}i ++;}endSortedArray = lastSwapIndex; //reset swap boundry}return;}GWILouisaxwzklaParticipantcould try:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : December,29,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 11:37:28 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < iostream ><br />//#include < string.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int input;<br />int numberOfInputItems;<br />int numberCount = 0;<br /><br />cout << "Enter number of items to input: ";<br />cin >> numberOfInputItems;<br /><br />int i = 0;<br />while ( i < numberOfInputItems )<br />{<br />cout << "enter a number: ";<br />cin >> input;<br />if ( input == 1 || input == 4 || input == 5 || input == 12 ||<br />input == 6 || input == 3 )<br />numberCount ++;<br />i ++;<br />}<br /><br />cout << endl;<br />cout << "The count was " << numberCount << endl;<br />}<br /><br />GWILouisaxwzklaParticipantI’m not sure what compiler your using but the include file ctype.h has a macro called isspace() that can be called on characters , like:
1234567<br />char ch = cin.get()<br />if ( isspace ( ch ) )<br />{<br />cout << "read a space" ;<br />}<br />The definition is listed as
The isspace() function returns non-zero if its argument is some sort of space (i.e. single space, tab, vertical tab, form feed, carriage return, or newline). Otherwise, zero is returned.
GWILouisaxwzklaParticipantYou pretty much got the idea about library functions. Most C/C++ compilers have a folder named “include” that has a number of “header” files that can be added to a project by using the preprocessor directive “#include” and then the header file in angle brackets ( like #include < iostream.h> ). There are a whole group of standardized header files each with a certain type of pre-built functions. stdio.h ( stands for standard input output ) , for example , has certain functions for input and outputing things to the screen and files. When you include a certaing “header file” you import all the functions in the file to your project , so this can add to your projects size. However , C/C++ is low level enough that you can write many of these functions your self instead of including them , or since many C/C++ have inline assembly language capabilties you can write your own custom set of utilities , if you desire. Many compilers come with executables that allow the user to compile and create their own header files as well. You’ll find C++ to be much more powerful than VB ……..
GWILouisaxwzklaParticipantcould just do this:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : September,9,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 6:23:04 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include < stdio.h ><br />#include < string.h ><br />#include < stdlib.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />char str [ 20 ];<br />char numberStr [ 10 ];<br /><br />strcpy ( str , "c:\data\file" );<br /><br />int stringLength = strlen ( str );<br /><br />int j = 0;<br />while ( j < 10 )<br />{<br /><br />itoa ( j , numberStr, 10 );<br />strcpy ( str + stringLength , numberStr );<br />printf ( "%s" , str );<br />printf ( "n" );<br />FILE * file = fopen ( str , "a" );<br />//..................<br />j ++;<br />}<br />return 0 ;<br />}<br /><br /><br />August 21, 2009 at 3:58 pm in reply to: Read 1st col vals of i/pfile;create o/p fl for each diff val #3606GWILouisaxwzklaParticipantHeres some code that does what you want ( maybe you can splice this into your program ):
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : August,10,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:17:50 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br /><br />#include < stdio.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />//using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />//CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!<br /><br />FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );<br /><br />FILE * outputFile;<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />char ch;<br />char newline = 10;<br />int commaCount;<br /><br /><br />char fileName [ 50 ] ;<br />strcpy ( fileName , "c:\programs\" );<br /><br /><br />fileName [ 12 ] = fgetc ( inputFile );<br />fileName [ 13 ] = fgetc ( inputFile );<br />fileName [ 14 ] = '.';<br />fileName [ 15 ] = 't';<br />fileName [ 16 ] = 'x';<br />fileName [ 17 ] = 't';<br />fileName [ 18 ] = 0;<br />fgetc ( inputFile );<br />while ( ! feof ( inputFile ) )<br />{<br /><br /><br /><br />ch = fgetc ( inputFile );<br />outputFile = fopen ( fileName , "a" );<br />commaCount = 0;<br />while ( ch != newline && commaCount < 4)<br />{<br />if ( ch == ',' )<br />commaCount ++;<br />else<br />fputc ( ch , outputFile );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( ' ' , outputFile );<br />//flush rest of line<br />while ( ch != newline )<br />ch = fgetc ( inputFile );<br />fclose ( outputFile );<br /><br /><br /><br />fileName [ 12 ] = fgetc ( inputFile );<br />fileName [ 13 ] = fgetc ( inputFile );<br />fileName [ 14 ] = '.';<br />fileName [ 15 ] = 't';<br />fileName [ 16 ] = 'x';<br />fileName [ 17 ] = 't';<br />fileName [ 18 ] = 0;<br />fgetc ( inputFile );<br />}<br /><br /><br />return 0 ;<br />}<br /><br /><br /><br />GWILouisaxwzklaParticipantheres an infix to postfix program I have laying around:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266<br /><br />#include < conio.h ><br />#include < math.h ><br />#include < string.h ><br />#include < stdlib.h ><br />#include < stdio.h ><br /><br />enum Boolean { False = 0 , True = 1 } ;<br />const int GREATER = 1 ;<br />const int NOTGREATER = 0 ;<br />int cnt = 0 , cntcol = 0 , postcnt = 0 ;<br /><br />//<hr class="bbcode_rule" />struct Stack<br />{<br />int info ;<br />Stack * previous;<br />} * top;<br /><br />//<hr class="bbcode_rule" />void Push ( int number )<br />{<br />Stack * current;<br />current = new ( Stack ) ;<br />if ( current == 0 )<br />{<br />printf ( "n allocation error!!!!! " );<br />return;<br />}<br />current -> info = number ;<br />current -> previous = top ;<br />top = current ;<br />}<br /><br />//<hr class="bbcode_rule" />int Pop ()<br />{<br />int temp ;<br />Stack * current = top;<br />temp = current -> info ;<br />top = top -> previous ;<br />delete current ;<br />return temp ;<br />}<br /><br />//<hr class="bbcode_rule" />int StackTop ()<br />{<br />return top -> info ;<br />}<br /><br />//<hr class="bbcode_rule" />Boolean isEmpty()<br />{<br />if ( top == NULL )<br />return True;<br />else<br />return False ;<br />}<br /><br />//<hr class="bbcode_rule" />Boolean isOperand ( char ch )<br />{<br />if ( ch >= '0' && ch <= '9' ) //if is a number<br />return True;<br />else<br />return False ;<br />}<br /><br />//<hr class="bbcode_rule" />void Infix ( char infix[][10])<br />{<br />char c ;<br />while ( c != 13 )<br />{<br />c = getch() ;<br />putch(c);<br />if ( c >= '0' && c <= '9' )<br />{<br />infix [cnt][cntcol] = c ;<br />cntcol++ ;<br />}<br />else if ( c == '+'|| c == '-'|| c == '*'|| c == '/')<br />{<br />infix [cnt][cntcol] = ' ' ;<br />cnt++ ;<br />infix [cnt][0] = c ;<br />infix [cnt][1] = ' ' ;<br />cnt++ ;<br />cntcol = 0 ;<br />}<br />else if ( c == 13 )<br />infix[cnt][cntcol] = ' ' ;<br />}<br />}<br /><br /><br />//<hr class="bbcode_rule" />int Calculate (int op1, char opr, int op2)<br />{<br />int ans ;<br />switch ( opr )<br />{<br />case '+' : ans = op1 + op2 ; break ;<br />case '-' : ans = op1 - op2 ; break ;<br />case '*' : ans = op1 * op2 ; break ;<br />case '/' : ans = op1 / op2 ; break ;<br />case '^' : ans = pow(op1, op2) ;<br />}<br />return ans ;<br />}<br />//<hr class="bbcode_rule" />int Evaluate ( char postfix[][10] )<br />{<br />for ( int i = 0 ; i <= cnt ; i ++ )<br />{<br />char ch = postfix<i>[0] ;<br />char ch1[10] ;<br />strcpy ( ch1 ,postfix </i><i> ) ;<br />// cout <<endl<< atoi ( ch1) ;<br />if ( isOperand ( ch ) == True )<br />Push ( atoi(ch1) );<br />else<br />{<br />int op2 = Pop () ;<br />int op1 = Pop () ;<br />int ans = Calculate ( op1, ch, op2 ) ;<br />// cout << ans << endl ;<br />Push ( ans ) ;<br />}<br />}<br />int ans = Pop () ;<br />return ans ;<br />}<br /><br />// </i><hr class="bbcode_rule" />int CheckP (char cur, char st)<br />{<br />int a = NOTGREATER ;<br /><br />if ( cur == '(' )<br />a = GREATER ;<br />else if ( cur != '(' && st == '(' )<br />a = GREATER ;<br />else if ( cur != '(' && st != '(' )<br />{<br />if ( ( cur == '*' || cur == '/' ) && ( st == '+' || st == '-' ) )<br />a = GREATER ;<br />}<br />return a ;<br /><br />}<br /><br /><br />//<hr class="bbcode_rule" />void infixToPostfix ( char infix[][10], char postfix[][10] )<br />{<br />int postcol = 0 ;<br />for ( int i = 0 ; i <= cnt ; i ++ )<br />{<br />// strcpy(postfix<i>, infix </i><i>) ;<br />// cout << postfix</i><i> ;<br />char ch;<br />ch = infix</i><i>[0] ;<br />if ( isOperand ( ch ) == True )<br />strcpy ( postfix [postcnt++] , infix </i><i> ) ;<br />else<br />{<br />if ( isEmpty() == True )<br />Push ( int(ch) ) ;<br />else<br />{<br />while ( CheckP( ch, StackTop() ) != GREATER && isEmpty() != True )<br />{<br />char c ;<br />if ( ch == ')' )<br />{<br />do<br />{<br />c = char ( Pop() ) ;<br />if ( c != '(')<br />{<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = ' ' ;<br />postcnt++ ;<br />}<br />}while ( c != '(' && isEmpty() != True);<br />break ;<br />}<br />else<br />c = char ( Pop() ) ;<br />if ( c != '(' && c != ')' )<br />{<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = ' ' ;<br />postcnt++ ;<br />}<br />}<br />if ( ch != ')' )<br />Push ( int(ch) ) ;<br />}<br />}<br />}<br /><br />while ( isEmpty () != True )<br />{<br />char c = char ( Pop () ) ;<br />postfix [postcnt][postcol] = c ;<br />postfix [postcnt][postcol+1] = ' ';<br />postcnt++ ;<br />}<br />// postfix[postcnt][postcol] = ' ';<br />}<br /><br />//</i><hr class="bbcode_rule" />void Postfix ( char postfix[][10] )<br />{<br />char ch[10] ;<br />for ( int i = 0; i <= postcnt; i++ )<br />{<br />strcpy(ch , postfix<i> ) ;<br />printf ( "%c" ,ch );<br />}<br />}<br /><br />// </i><hr class="bbcode_rule" />void main()<br />{<br />//clrscr();<br />top = NULL ;<br /><br />char infix [ 50 ][ 10 ] ;<br />char postfix [ 50 ][ 10 ] ;<br /><br />//gotoxy ( 5, 8 ) ;<br />printf ( "Enter Arithematic Expression ...................... : " );<br />Infix ( infix ) ;<br /><br />infixToPostfix ( infix, postfix ) ;<br />//gotoxy ( 5, 10 ) ;<br />printf ( "Arithematic Expression in Postfix Notation......... : " );<br />Postfix ( postfix ) ;<br />int ans = Evaluate ( postfix ) ;<br />//gotoxy (5, 11 ) ;<br />printf ( "The Answer of Expression .......................... : " );<br />printf ( "%i", ans );<br /><br />getch();<br />}<br />//<hr class="bbcode_rule" />GWILouisaxwzklaParticipantHeres a linked list program I had laying around:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303<br />/****************************************************************<br />* File Name : cgt.cpp<br />* Date : April,10,2007<br />* Comments : new project<br />* Compiler/Assembler : VC++ 6.0<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:59:58<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br />struct node<br />{<br /><br />int data;<br />node * next;<br />node * back;<br /><br />};<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void readList ( node ** list );<br />bool listsAreEqual ( node * left , node * right );<br />node * copyList ( node * list );<br />void printList ( node * list );<br />void destroyList ( node ** list );<br />bool listsContainEqualMembers ( node * left , node * right );<br /><br />//##################################################################################<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />node * head1 = 0;<br />node * head2 = 0;<br /><br />cout << "enter first list :" << endl;<br />readList ( & head1 );<br />cout << "enter second list: " << endl;<br />readList ( & head2 );<br /><br />if ( listsAreEqual ( head1 , head2 ) )<br />cout << endl << "The lists are equal" << endl;<br />else<br />cout << endl << "The lists are not equal" << endl;<br /><br />cout << endl;<br />if ( listsContainEqualMembers ( head1 , head2 ) )<br />cout << endl << "lists contain equal members" << endl;<br />else<br />cout << endl << "lists do not contain equal members " << endl;<br /><br /><br />node * copy = copyList ( head1 );<br />cout << endl << "copy of first list is: ";<br />printList ( copy );<br />destroyList ( & head1 );<br />destroyList ( & head2 );<br />destroyList ( & copy );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : readList<br />Parameters :<br /><br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void readList ( node ** list )<br />{<br /><br />cout << "enter an integer ( -1 to stop ) ";<br />int i;<br />cin >> i;<br />node * temp = 0;<br /><br />while ( i != -1 )<br />{<br /><br />temp = new node;<br />if ( temp == 0 )<br />{<br />cout << " allocation error" ;<br />( * list ) = 0;<br />return;<br />}<br />temp -> data = i ;<br />if ( ( * list ) == 0 )<br />{<br />( * list ) = temp;<br />temp -> next = 0;<br />temp -> back = temp;<br /><br />}<br />else<br />{<br />( * list ) -> back -> next = temp;<br />temp -> next = temp -> back = 0;<br />( * list ) -> back = temp;<br />}<br />cout << "enter a number ( -1 to stop ) ";<br />cin >> i;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : listsAreEqual<br />Parameters :<br /><br /><br /><br />Returns: user defined type , bool<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool listsAreEqual ( node * left , node * right )<br />{<br /><br />bool matches = true;<br /><br />while ( left != 0 && right != 0 && matches )<br />{<br /><br />if ( left -> data != right -> data )<br />matches = false;<br />else<br />{<br />left = left -> next;<br />right = right -> next;<br />}<br />}<br />if ( ! left && ! right && matches )<br />return true;<br /><br />return false;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : copyList<br />Parameters :<br /><br /><br /><br />Returns: user defined type , node *<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />node * copyList ( node * list )<br />{<br /><br />node * temp = 0;<br />node * newList = 0;<br /><br />while ( list )<br />{<br /><br />temp = new node;<br />if ( ! temp )<br />{<br />cout << "allocation error! ";<br />return 0;<br />}<br />if ( newList == 0 )<br />{<br />newList = temp;<br />temp -> data = list -> data;<br />newList -> next = 0;<br />newList -> back = temp;<br />}<br />else<br />{<br />temp -> data = list -> data;<br />newList -> back -> next = temp;<br />newList -> back = temp;<br />temp -> next = temp -> next = 0;<br />}<br />list = list -> next;<br />}<br />return newList;<br />}<br />void printList ( node * temp )<br />{<br /><br /><br />cout << "list read is " ;<br />while ( temp )<br />{<br />cout < data << " ";<br />temp = temp -> next;<br />}<br />cout << endl;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : destroyList<br />Parameters :<br /><br /><br /><br />Returns: user defined type , void<br /><br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void destroyList ( node ** list )<br />{<br /><br /><br />node * temp = ( * list );<br />node * temp2 = 0;<br />while ( temp )<br />{<br /><br />temp2 = temp;<br />temp = temp -> next;<br />delete temp2;<br />}<br /><br />( * list ) = 0;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : listsContainEqualMembers<br />Parameters :<br /><br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />bool listsContainEqualMembers ( node * left , node * right )<br />{<br /><br /><br />node * temp1 = left;<br />node * temp2 = right;<br />bool matches ;<br />int length1 = 0 , length2 = 0;<br />int numItems1 = 0 , numItems2 = 0;<br /><br />while ( temp1 && matches )<br />{<br />temp2 = right;<br />matches = false;<br />length1 ++; //get length of first list<br />while ( temp2 && ! matches )<br />{<br />if ( temp1 -> data == temp2 -> data )<br />matches = true;<br />if ( ! matches )<br />temp2 = temp2 -> next;<br />}<br />if ( matches )<br />temp1 = temp1 -> next;<br />}<br />temp2 = right;<br />while ( temp2 )<br />{<br />length2 ++ ;<br />temp2 = temp2 -> next;<br />}<br />if ( length1 == length2 && matches )<br />return true;<br />return false;<br />}<br /><br />heres another one:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153<br /><br /><br />#include <br />#include <br />//simple list<br /><br />typedef struct nodeType<br />{<br />int i;<br />struct nodeType * next;<br />} node;<br /><br />void print ( node * );<br />void addBack ( int , node ** , node ** );<br />void addFront ( int , node ** , node ** );<br />void destroy ( node ** );<br /><br />void main()<br />{<br /><br />node * head = NULL;<br />node * tail = NULL;<br />char choice;<br />char newline;<br />int i;<br />do<br />{<br />printf ( "choose an operation: n" );<br />printf ( "1. add a node to back of list n" );<br />printf ( "2. add a node to front of list n" );<br />printf ( "3. print the list. n" );<br />printf ( "4. exit n" );<br />choice = getchar ();<br />newline = getchar ();<br />printf ( "n" );<br />switch ( choice )<br />{<br />case '1':<br />{<br />printf ( "enter an integer : n" );<br />scanf ( "%d",& i );<br />addBack ( i , & head , & tail );<br />newline = getchar ();<br />};<br />break;<br />case '2':<br />{<br />printf ( "enter an integer : n" );<br />scanf ( "%d",& i );<br />addFront ( i , & head , & tail );<br />newline = getchar ();<br />};<br />break;<br />case '3':<br />{<br />print ( head );<br />};<br />break;<br />case '4':<br />{<br />printf ( "goodbye! n" );<br />};<br />break;<br />default: printf ( "bad choice ! n" );<br /><br />};<br />printf ( "n" );<br />}<br />while ( choice != '4' );<br />destroy ( & head );<br /><br /><br /><br /><br /><br />}<br /><br />void print ( node * r )<br />{<br /><br />node * t = r;<br /><br />printf ( "This is the list: n" );<br /><br />while ( t != NULL )<br />{<br />printf ( "%c" ," " );<br />printf ( "%i" , t -> i );<br />printf ( "%c" , " " );<br />t = t -> next;<br />}<br />printf ( "n" );<br /><br />}<br /><br />void addBack ( int n , node ** h , node ** t )<br />{<br /><br />if ( ( * h ) == NULL )<br />{<br />( * h ) = ( * t ) = malloc ( sizeof ( node ) );<br />( * t ) -> i = n;<br />( * t ) -> next = NULL;<br /><br />}<br />else<br />{<br /><br />( * t ) -> next = malloc ( sizeof ( node ) );<br />( * t ) -> next -> i = n;<br />( * t ) -> next -> next = NULL;<br />( * t ) = ( * t ) -> next;<br /><br />}<br />}<br /><br />void addFront ( int n , node ** h , node ** t )<br />{<br /><br />if ( ( * h ) == NULL ) //empty list<br />{<br /><br />( * h ) = ( * t ) = malloc ( sizeof ( node ) );<br />( * t ) -> i = n;<br />( * t ) -> next = NULL;<br /><br />}<br />else //add to front<br />{<br />node * temp = ( * h );<br /><br />( * h ) = malloc ( sizeof ( node ) );<br />( * h ) -> i = n;<br />( * h ) -> next = temp;<br />}<br />}<br /><br />void destroy ( node ** h )<br />{<br /><br />node * temp;<br /><br />while ( ( * h ) != NULL )<br />{<br />temp = ( * h );<br />( * h ) = ( * h ) -> next;<br />free ( temp );<br />}<br />}<br /><br /><br />GWILouisaxwzklaParticipantcould try:
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : August,11,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 2:00:25 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int numberYears;<br />float interestRate;<br />int initialAmount;<br /><br />cout << "enter intial amount: ";<br />cin >> initialAmount;<br /><br />cout << "enter the interest rate: ( rate % ) " ;<br />cin >> interestRate;<br />interestRate = interestRate / 100;<br />cin.get ();<br /><br />cout << "enter number of years: ";<br />cin >> numberYears;<br /><br />cout << "amount plus interest: ";<br />cout << initialAmount + ( ( interestRate * initialAmount ) * numberYears ) ;<br />cout << endl;<br /><br /><br /><br />return 0 ;<br />}<br /><br /><br /><br /><br />output:
12345678<br />enter intial amount: 100<br />enter the interest rate: ( rate % ) 10%<br />enter number of years: 2<br />amount plus interest: 120<br />Press any key to continue<br /><br />August 11, 2009 at 6:00 pm in reply to: Reading first column in a file and creating new files… #3601GWILouisaxwzklaParticipantThis seems to work:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : August,10,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:17:50 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />//CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!<br /><br />FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );<br /><br />FILE * outputFile1 = fopen ( "c:\programs\data2.txt", "w+" );<br />FILE * outputFile2 = fopen ( "c:\programs\data3.txt", "w+" );<br />FILE * outputFile3 = fopen ( "c:\programs\data4.txt", "w+" );<br />FILE * outputFile4 = fopen ( "c:\programs\data5.txt", "w+" );<br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />char ch;<br />char newline = 10;<br /><br />ch = fgetc ( inputFile );<br />fgetc ( inputFile );<br />while ( ! feof ( inputFile ) )<br />{<br /><br />if ( ch == 'A' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile1 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile1);<br />}<br />if ( ch == 'B' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile2 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile2 );<br />}<br />if ( ch == 'C' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile3 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile3);<br />}<br />if ( ch == 'D' )<br />{<br />ch = fgetc ( inputFile );<br />while ( ch != newline )<br />{<br />fputc ( ch , outputFile4 );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( 'n' , outputFile4);<br />}<br /><br />ch = fgetc ( inputFile );<br />fgetc ( inputFile );<br />}<br />fclose ( outputFile1 );<br />fclose ( outputFile2 );<br />fclose ( outputFile3 );<br />fclose ( outputFile4 );<br /><br />return 0 ;<br />}<br /><br /><br /><br /><br />GWILouisaxwzklaParticipantcould try this:
1234567891011121314151617181920212223<br />#include <br /><br />void main()<br />{<br /><br />int i;<br />cout << "Enter a number to convert ";<br />cin >> i;<br />int binaryArray [ 20 ] ; //whatever<br />int j = 0;<br />while ( i )<br />{<br />binaryArray [ j ++ ] = i % 2 ;<br />i /= 2;<br />}<br /><br />cout << endl << "Binary Version: " ;<br />while ( j -- > 0 )<br />cout << binaryArray [ j ];<br />cout << endl;<br />}<br />July 29, 2009 at 9:23 pm in reply to: c program for sort and remove duplicate values from an array #3599GWILouisaxwzklaParticipantWhat type of sort are you using ? I would just sort , count unique instances , allocate enough memory to hold the unique instances and copy the data ……
GWILouisaxwzklaParticipantA little correction. It seems you are passing arrays of names , id’s and crn into the function:
123<br />void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);<br />so you would change your prototype to :
1234<br />const int numberOfStudents = 20; //whatever<br />void addStudent ( STUDENT newStudent [ numberOfStudents ] );<br />and access the data members like this:
1234567<br />..........<br />int i = currentStudent;<br />printf ( newStudent [ currentStudent ].name ); //output the student name<br />printf ( "%i" , newStudent [ currentStudent ] .id ); //output the student id<br /><br />my bad ………
GWILouisaxwzklaParticipantYou really just need to group all your data together ( structs are handy for this purpose ). So if you have a function prototype like:
123<br />void addStudent (char name[SIZE][LENGTH], int id[SIZE], int crn[SIZE][4]);<br />change it to:
123<br />void addStudent ( STUDENT newStudent );<br />when you need to access a member of the struct , just use the dot operator and use the data as usual:
12345<br />..........<br />printf ( newStudent.name ); //output the student name<br />printf ( "%i" , newStudent.id ); //output the student id<br />not too much to the conversion …….
GWILouisaxwzklaParticipantHeres the basic program that creates a deck and draws a hand ( not sure what you need to do as far as identifing the type of hand ?? You need the program to tell if the hand falls in one of the catagories : ( high card, pair, three of a kind, full house, straight, flush, four of a kind, straight flush, and royal flush.” ) ?? . Anyway heres the code ( not very complicated just tedious to write ) :
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : July,27,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 12:13:49 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br /><br />enum type { one , two , three , four , five , six , seven , eight , nine , ten , jack , queen , king };<br />enum suite { clubs , diamonds , hearts , spades };<br /><br /><br />struct card<br />{<br /><br />type cardType;<br />suite cardSuite;<br />bool drawn;<br />void initialize ( type thisCardType , suite thisCardSuite );<br />};<br /><br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void initializeDeck ( card * deck );<br />int drawCard ( card * deck );<br />void printHand ( card * hand );<br />void createHand ( card * hand , card * deck );<br />void rebuildDeck ( card * deck );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />card deck [ 52 ];<br />card hand [ 5 ];<br /><br />initializeDeck ( deck );<br />createHand ( hand , deck );<br />printHand ( hand );<br /><br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : initialize<br />Parameters :<br /><br />thisCardType a(n) type ( type ) ,<br />thisCardSuite a(n) suite ( suite )<br /><br /><br />Class/Struct associations : struct card<br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void card :: initialize ( type thisCardType , suite thisCardSuite )<br />{<br /><br />cardType = thisCardType;<br />cardSuite = thisCardSuite;<br />drawn = false;<br />return;<br />}<br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : initializeDeck<br />Parameters :<br /><br />deck a(n) cards * ( cards * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void initializeDeck ( card * deck )<br />{<br /><br />deck [ 0 ].initialize ( one , clubs);<br />deck [ 1 ].initialize ( two , clubs );<br />deck [ 2 ].initialize ( three , clubs );<br />deck [ 3 ].initialize ( four , clubs );<br />deck [ 4 ].initialize ( five , clubs );<br />deck [ 5 ].initialize ( six , clubs );<br />deck [ 6 ].initialize ( seven , clubs);<br />deck [ 7 ].initialize ( eight , clubs );<br />deck [ 8 ].initialize ( nine , clubs);<br />deck [ 9 ].initialize ( ten , clubs);<br />deck [ 10 ].initialize ( jack , clubs );<br />deck [ 11 ].initialize ( queen ,clubs );<br />deck [ 12 ].initialize ( king , clubs );<br />deck [ 13 ].initialize ( one , diamonds );<br />deck [ 14 ].initialize ( two , diamonds );<br />deck [ 15 ].initialize ( three , diamonds );<br />deck [ 16 ].initialize ( four , diamonds );<br />deck [ 17 ].initialize ( five , diamonds );<br />deck [ 18 ].initialize ( six , diamonds );<br />deck [ 19 ].initialize ( seven , diamonds );<br />deck [ 20 ].initialize ( eight , diamonds );<br />deck [ 21 ].initialize ( nine , diamonds );<br />deck [ 22 ].initialize ( ten , diamonds );<br />deck [ 23 ].initialize ( jack , diamonds );<br />deck [ 24 ].initialize ( queen , diamonds );<br />deck [ 25 ].initialize ( king , diamonds );<br />deck [ 26 ].initialize ( one , hearts );<br />deck [ 27 ].initialize ( two , hearts );<br />deck [ 28 ].initialize ( three , hearts );<br />deck [ 29 ].initialize ( four , hearts );<br />deck [ 30 ].initialize ( five , hearts );<br />deck [ 31 ].initialize ( six , hearts );<br />deck [ 32 ].initialize ( seven , hearts );<br />deck [ 33 ].initialize ( eight , hearts );<br />deck [ 34 ].initialize ( nine , hearts );<br />deck [ 35 ].initialize ( ten , hearts );<br />deck [ 36 ].initialize ( jack , hearts );<br />deck [ 37 ].initialize ( queen , hearts );<br />deck [ 38 ].initialize ( king , hearts );<br />deck [ 39 ].initialize ( one , spades );<br />deck [ 40 ].initialize ( two , spades );<br />deck [ 41 ].initialize ( three , spades );<br />deck [ 42 ].initialize ( four , spades );<br />deck [ 43 ].initialize ( five , spades );<br />deck [ 44 ].initialize ( six , spades );<br />deck [ 45 ].initialize ( seven , spades );<br />deck [ 46 ].initialize ( eight ,spades );<br />deck [ 47 ].initialize ( nine , spades );<br />deck [ 48 ].initialize ( ten , spades );<br />deck [ 49 ].initialize ( jack , spades );<br />deck [ 50 ].initialize ( queen , spades );<br />deck [ 51 ].initialize ( king , spades );<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : drawCard<br />Parameters :<br /><br />deck a(n) cards * ( cards * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />int drawCard ( card * deck )<br />{<br />srand ( time(NULL) );<br /><br /><br />int i = rand () % 52;<br />while ( deck [ i ].drawn == true )<br />{<br /><br />i = rand () % 52;<br /><br />}<br />deck [ i ].drawn = true;<br /><br />return i;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : printHand<br />Parameters :<br /><br />hand a(n) cards * ( cards * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void printHand ( card * hand )<br />{<br />int i = 0;<br /><br />cout << "hand drawn :" << endl;<br /><br />while ( i < 5 )<br />{<br /><br />cout << " ";<br />switch ( hand [ i ].cardType )<br />{<br />case one:<br />{<br />cout << "one ";<br />};<br />break;<br />case two:<br />{<br />cout << "two ";<br />};<br />break;<br />case three:<br />{<br />cout << "three ";<br />};<br />break;<br />case four:<br />{<br />cout << "four ";<br />};<br />break;<br />case five:<br />{<br />cout << "five ";<br />};<br />break;<br />case six:<br />{<br />cout << "six ";<br />};<br />break;<br />case seven:<br />{<br />cout << "seven ";<br />};<br />break;<br />case eight:<br />{<br />cout << "eight ";<br />};<br />break;<br />case nine:<br />{<br />cout << "nine ";<br />};<br />break;<br />case ten:<br />{<br />cout << "ten ";<br />};<br />break;<br />case jack:<br />{<br />cout << "jack ";<br />};<br />break;<br />case queen:<br />{<br />cout << "queen ";<br />};<br />break;<br />case king:<br />{<br />cout << "king ";<br />};<br />break;<br />};<br />cout << "of ";<br />switch ( hand [ i ].cardSuite )<br />{<br />case clubs:<br />{<br />cout << "clubs" << endl;<br />};<br />break;<br />case diamonds:<br />{<br />cout << "diamonds" << endl;<br />};<br />break;<br />case hearts:<br />{<br />cout << "hearts" << endl;<br />};<br />break;<br />case spades:<br />{<br />cout << "spades" << endl;<br />};<br />break;<br />};<br />i ++;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : createHand<br />Parameters :<br /><br />hand a(n) card * ( card * ) ,<br />deck a(n) card * ( card * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void createHand ( card * hand , card * deck )<br />{<br /><br />int i = 0;<br />int cardIndex;<br /><br />while ( i < 5 )<br />{<br />cardIndex = drawCard ( deck );<br />hand [ i ].cardType = deck [ cardIndex ].cardType;<br />hand [ i ].cardSuite = deck [ cardIndex ].cardSuite;<br />i ++;<br />}<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : rebuildDeck<br />Parameters :<br /><br />deck a(n) card * ( card * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void rebuildDeck ( card * deck )<br />{<br /><br />int i = 0;<br /><br />while ( i < 52 )<br />{<br />deck [ i ].drawn = false;<br />i ++;<br />}<br /><br /><br />return;<br />}<br /><br /><br /> - AuthorPosts