Forum Replies Created
- AuthorPosts
- GWILouisaxwzklaParticipant
could try:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : july,21,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:05:56 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />#include <br />//#include <br />//#include <br />//#include <br /><br />const MAX_SIZE = 10;<br />const int MAX_NUMBERS = 10;<br />using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int numbers [ MAX_SIZE ];<br />int i;<br />int smallest , largest;<br />i = 1;<br />srand ( time ( NULL ) ); //seed random number generator<br />numbers [ 0 ] = rand () % MAX_NUMBERS;<br />smallest = largest = numbers [ 0 ]; //set smallest and largest for comparison<br />//generate random numbers<br />while ( i < MAX_SIZE )<br />{<br />numbers [ i ] = rand () % MAX_NUMBERS;<br />if ( numbers [ i ] > largest )<br />largest = numbers [ i ];<br />else if ( numbers [ i ] < smallest )<br />smallest = numbers [ i ];<br />i ++;<br />}<br />cout << "array generated: " << endl;<br />i = 0;<br />while ( i < MAX_NUMBERS )<br />{<br />cout << numbers [ i ] << " ";<br />i ++;<br />}<br />cout << endl << endl;<br />cout << "largest number was " << largest << endl;<br />cout << "smallest number was " << smallest << endl;<br /><br />return 0 ;<br />}<br /><br /><br /><br />GWILouisaxwzklaParticipantI haven’t used the software since I’ve gotten pretty good at finding access violations after years of writing my own data structures and lots of software that uses pointers. Anything I write in C/C++ or assembly language is designed with execution speed in mind so I probably would not use anything that slows my code down ( I belive that is why C/C++ was designed without provisions to check for access violations at compile time ). Anyway , I guess the software is a clever idea and if you don’t mind the performance hit , use it ………
GWILouisaxwzklaParticipantInteresting program ( I suppose this software is based on a parser that uses the C/C++ grammer that checks declarations and uses of pointers and arrays ). I guess its fine to use something like this ( if you trust the authors code :) ). You do have to run your source code through two translators per compile though , this could take extra time with large programs …..
GWILouisaxwzklaParticipantcould do something like this:
123456789101112131415161718192021222324252627282930313233<br />include <stdio.h><br />#define MAX 5<br /><br />int main()<br /><br />{<br /><br />int i = 1;<br />int number = 0, highNum = 0;<br />int highPosition = 0;<br /><br />printf("This program will choose the largest number entered.");<br />printf("nWe are going to enter five numbers.nn");<br /><br />for (i = 1; i <= MAX; i++)<br />{<br />printf("Enter an integer: ");<br />scanf("%d", &number);<br /><br />if (number > highNum)<br />{<br />highNum = number;<br />highPosition = i;<br />}<br />}<br /><br />printf("nThe largest number entered was %dn", highNum);<br />printf("nThe number was in position %dn", highPosition );<br />return 0;<br />}<br /><br />GWILouisaxwzklaParticipantHeres a bubble sort I wrote awhile ago ( hope this helps ):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : March,28,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />* Notes: A good sort for a small number of items. Total run time<br />* is proportional to the number of items squared ( o ( N^2 ) ).<br />*<br />*<br />* Program Shell Generated At: 11:25:48 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 />#include <stdlib.h><br />#include <time.h><br /><br />using namespace std;<br /><br />//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br />const int MAX_NUMBERS = 50;<br />int NUMBERS_PER_LINE = 15;<br />//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /><br /><br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void generateNumbers ( int * numbers , int numberItems );<br />void bubbleSort ( int * array , int numberItems );<br />void printArray ( int * array , int numberItems );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int array [ MAX_NUMBERS ];<br />generateNumbers ( array , MAX_NUMBERS );<br />cout << endl << endl;<br />cout << "unsorted array " << endl;<br />printArray ( array , MAX_NUMBERS );<br />bubbleSort ( array , MAX_NUMBERS );<br />cout << endl << endl << "sorted array: " << endl;<br />printArray ( array , MAX_NUMBERS );<br />cout << endl;<br /><br /><br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : bubbleSort<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void bubbleSort ( int * array , int numberItems )<br />{<br /><br />int endSortedArray = numberItems - 1;<br />int lastSwapIndex;<br />int temp;<br />while ( endSortedArray > 0 ) //while not at the end of the unsorted array<br />{<br />lastSwapIndex = 0; //save index of the last item swapped<br />int i = 0; //start at the beggining of the unsorted array<br />while ( i < endSortedArray ) //while not in the sorted items<br />{<br />if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up<br />{<br />//swap array [ i ] and array [ i + 1 ]<br />temp = array [ i ];<br />array [ i ] = array [ i + 1 ];<br />array [ i + 1 ] = temp;<br />lastSwapIndex = i;<br />}<br />i ++;<br />}<br />endSortedArray = lastSwapIndex; //reset swap boundry<br />}<br /><br /><br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : printArray<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void printArray ( int * array , int numberItems )<br />{<br /><br /><br />int i = 0;<br />cout << endl << endl;<br />cout << "array : " << endl;<br />while ( i < numberItems )<br />{<br />cout << array [ i ] << " " ;<br />if ( i != 0 )<br />if ( i % NUMBERS_PER_LINE == 0 )<br />cout << endl;<br />i ++;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : generateNumbers<br />Parameters :<br /><br />numbers a(n) int ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void generateNumbers ( int * numbers , int numberItems )<br />{<br /><br />int i = 0;<br /><br />srand ( time ( NULL ) );<br />while ( i < numberItems )<br />{<br />numbers [ i ] = rand () % MAX_NUMBERS;<br />i ++;<br />}<br /><br /><br />return;<br />}<br />GWILouisaxwzklaParticipantThe best thing to do in c/c++ is check that the indexes are with in range :
12345<br />int array [ 2] [ 2 ];<br />if ( i < 2 && j < 2 )<br />var = array [ i ] [ j ];<br />since c/c++ does not use array descriptors ( an array header with the maximum length information ) there is really no other way I can think of.
GWILouisaxwzklaParticipantThis is really the only way to do this in C ( if your decision was based on constants I would suggest a “switch” statement ). You could could also use “goto”‘s and lables but this would be bad programming practice and the compiler will generate the same type of code at the assembly level anyway. You could also use “function pointers” in an array and make the variable ‘n’ index this array and then call the proper function , but this might be a waste of memory even thought you avoid testing a value for each “if” statement. I’d probably stick with the “if” chain ……..
GWILouisaxwzklaParticipantHeres a good tutorial on C++ exception handling : http://www.deitel.com/articles/cplusplus_tutorials/20060325/
GWILouisaxwzklaParticipantfirst part:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : June,2,2009<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 12:54:06 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 />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />int binarySearch ( int * array , int itemToFind , int maxItems );<br />int sequentialSearch ( int * array , int itemToFind , int maxItems );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br />int array [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 ,7 , 8 , 9 , 10 };<br />int indexFoundAt;<br />int itemToFind ;<br /><br />printf ( "enter an item to find ( 1 to 10 ) n" );<br />scanf ( "%i" , & itemToFind );<br /><br /><br /><br />indexFoundAt = binarySearch ( array , itemToFind , 10 );<br />printf ( "The item %i was found at index %i n" , itemToFind , indexFoundAt );<br />indexFoundAt = sequentialSearch ( array , itemToFind , 10 );<br />printf ( "The item %i was found at index %i n" , itemToFind , indexFoundAt );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : binarySearch<br />Parameters :<br /><br />array a(n) int * ,<br />maxItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />int binarySearch ( int * array , int itemToFind , int maxItems )<br />{<br /><br />int left , right;<br />int midPoint = ( ( maxItems - 1 ) + 0 ) / 2;<br /><br /><br />left = 0;<br />right = maxItems - 1;<br /><br />while ( array [ midPoint ] != itemToFind && right > left )<br />{<br />if ( array [ midPoint ] > itemToFind )<br />{<br />right = midPoint - 1;<br />midPoint = ( right + left ) / 2;<br />}<br />else<br />{<br />left = midPoint + 1;<br />midPoint = ( right + left ) / 2;<br />}<br /><br />}<br />if ( array [ midPoint ] == itemToFind )<br />return midPoint;<br />else<br />return -1; //not found<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : sequentialSearch<br />Parameters :<br /><br />array a(n) int * ,<br />maxItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />int sequentialSearch ( int * array , int itemToFind , int maxItems )<br />{<br /><br />int i = 0;<br /><br />while ( i < maxItems && array [ i ] != itemToFind )<br />i ++;<br /><br />if ( array [ i ] == itemToFind )<br />return i;<br />else<br />return -1; //not found<br />}<br /><br /><br /><br /><br />second part:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : June,2,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 1:42:17 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />//using namespace std;<br /><br />struct node<br />{<br /><br />int data;<br />node * prior;<br />node * next;<br />};<br /><br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void addNodeFront ( int item , node ** frontNode , node ** backNode );<br />void addNodeBack ( int item , node ** frontNode , node ** backNode );<br />void printList ( node * front );<br />void destroyList ( node * front );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />node * front = 0;<br />node * back = 0;<br /><br /><br />addNodeFront ( 1 , & front , & back );<br />addNodeBack ( 2 , & back , & back );<br />printList ( front );<br />destroyList ( front );<br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : addNodeFront<br />Parameters :<br /><br />item a(n) int ,<br />frontNode a(n) node ** ( node ** )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void addNodeFront ( int item , node ** frontNode , node ** backNode )<br />{<br /><br />node * temp = ( node * ) malloc ( sizeof ( node ) );<br />if ( temp == 0 )<br />{<br />printf ( "allocation error n " );<br />return;<br />}<br /><br />if ( * frontNode == 0 ) //empty list<br />{<br />temp -> data = item;<br />temp -> prior = 0;<br />temp -> next = 0;<br />* frontNode = temp;<br />* backNode = temp;<br />}<br />else //one node<br />{<br />temp -> data = item;<br />temp -> next = * frontNode;<br />( * frontNode ) -> prior = temp;<br />temp -> prior = 0;<br />* frontNode = temp;<br />}<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : addNodeBack<br />Parameters :<br /><br />item a(n) int ,<br />backNode a(n) node ** ( node ** )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void addNodeBack ( int item , node ** frontNode , node ** backNode )<br />{<br /><br />node * temp = ( node * ) malloc ( sizeof ( node ) );<br />if ( temp == 0 )<br />{<br />printf ( "allocation error n " );<br />return;<br />}<br /><br />if ( * frontNode == 0 ) //empty list<br />{<br />temp -> data = item;<br />temp -> prior = 0;<br />temp -> next = 0;<br />* frontNode = temp;<br />* backNode = temp;<br />}<br />else //one node<br />{<br />temp -> data = item;<br />temp -> next = 0;<br />( * backNode ) -> next = temp;<br />temp -> prior = * backNode;<br />* backNode = temp;<br />}<br />return;<br /><br /><br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : printList<br />Parameters :<br /><br />front a(n) node * ( node * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void printList ( node * front )<br />{<br /><br />node * temp = front;<br /><br />while ( temp != 0 )<br />{<br />printf ("%i " , temp -> data , " " );<br />temp = temp -> next;<br /><br />}<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : destroyList<br />Parameters :<br /><br />front a(n) node * ( node * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void destroyList ( node * front )<br />{<br /><br />node * temp = front;<br /><br />while ( temp != 0 )<br />{<br />front = temp -> next;<br />free ( temp );<br />temp = front;<br />}<br /><br />return;<br />}<br /><br />bubble sort:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : March,28,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />* Notes: A good sort for a small number of items. Total run time<br />* is proportional to the number of items squared ( o ( N^2 ) ).<br />*<br />*<br />* Program Shell Generated At: 11:25:48 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br />#include <br />#include <br /><br />using namespace std;<br /><br />//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br />const int MAX_NUMBERS = 50;<br />int NUMBERS_PER_LINE = 15;<br />//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /><br /><br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void generateNumbers ( int * numbers , int numberItems );<br />void bubbleSort ( int * array , int numberItems );<br />void printArray ( int * array , int numberItems );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int array [ MAX_NUMBERS ];<br />generateNumbers ( array , MAX_NUMBERS );<br />cout << endl << endl;<br />cout << "unsorted array " << endl;<br />printArray ( array , MAX_NUMBERS );<br />bubbleSort ( array , MAX_NUMBERS );<br />cout << endl << endl << "sorted array: " << endl;<br />printArray ( array , MAX_NUMBERS );<br />cout << endl;<br /><br /><br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : bubbleSort<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void bubbleSort ( int * array , int numberItems )<br />{<br /><br />int endSortedArray = numberItems - 1;<br />int lastSwapIndex;<br />int temp;<br />while ( endSortedArray > 0 ) //while not at the end of the unsorted array<br />{<br />lastSwapIndex = 0; //save index of the last item swapped<br />int i = 0; //start at the beggining of the unsorted array<br />while ( i < endSortedArray ) //while not in the sorted items<br />{<br />if ( array [ i ] > array [ i + 1 ] ) //if current item is smaller than next , bubble up<br />{<br />//swap array [ i ] and array [ i + 1 ]<br />temp = array [ i ];<br />array [ i ] = array [ i + 1 ];<br />array [ i + 1 ] = temp;<br />lastSwapIndex = i;<br />}<br />i ++;<br />}<br />endSortedArray = lastSwapIndex; //reset swap boundry<br />}<br /><br /><br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : printArray<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void printArray ( int * array , int numberItems )<br />{<br /><br /><br />int i = 0;<br />cout << endl << endl;<br />cout << "array : " << endl;<br />while ( i < numberItems )<br />{<br />cout << array [ i ] << " " ;<br />if ( i != 0 )<br />if ( i % NUMBERS_PER_LINE == 0 )<br />cout << endl;<br />i ++;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : generateNumbers<br />Parameters :<br /><br />numbers a(n) int ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void generateNumbers ( int * numbers , int numberItems )<br />{<br /><br />int i = 0;<br /><br />srand ( time ( NULL ) );<br />while ( i < numberItems )<br />{<br />numbers [ i ] = rand () % MAX_NUMBERS;<br />i ++;<br />}<br /><br /><br />return;<br />}<br /><br /><br />selection sort:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : March,28,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />* Notes: A good sort for a small number of items. Total run time<br />* is proportional to the number of items squared ( o ( N^2 ) ).<br />*<br />*<br />* Program Shell Generated At: 11:25:48 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br />//#include <br />#include <br />#include <br /><br />using namespace std;<br /><br />//>>>>>>>>>>>>>>>>>>>>>>>> GLOBAL DATA <<<<<<<<<<<<<<<<<<<<<<<<<<br />const int MAX_NUMBERS = 50;<br />int NUMBERS_PER_LINE = 15;<br />//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<br /><br /><br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void generateNumbers ( int * numbers , int numberItems );<br />void selectionSort ( int * array , int numberItems );<br />void printArray ( int * array , int numberItems );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />int array [ MAX_NUMBERS ];<br />generateNumbers ( array , MAX_NUMBERS );<br />cout << endl << endl;<br />cout << "unsorted array " << endl;<br />printArray ( array , MAX_NUMBERS );<br />selectionSort ( array , MAX_NUMBERS );<br />cout << "sorted array: " << endl;<br />printArray ( array , MAX_NUMBERS );<br />cout << endl;<br /><br /><br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : selectionSort<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments: Slow n^2 algorithm<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void selectionSort ( int * array , int numberItems )<br />{<br /><br />int i = 0 , j , minimumIndex , temp;<br /><br />while ( i <= numberItems )<br />{<br />minimumIndex = i; //select i to be smallest index<br />j = minimumIndex + 1; //start sorting at next index after<br />while ( j <= numberItems )<br />{<br />if ( array [ j ] < array [ minimumIndex ] ) //if we have found a smaller than minimum<br />{ //element , mark current as smallest<br />minimumIndex = j;<br />}<br />j ++; //goto next item in the array<br />}<br />if ( minimumIndex != numberItems ) //if we found a smaller item put it at the front<br />{ //of the sorted array<br />temp = array [ minimumIndex ]; //swap items<br />array [ minimumIndex ] = array [ i ];<br />array [ i ] = temp;<br />}<br />i ++; //goto next item in the array<br />}<br /><br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : printArray<br />Parameters :<br /><br />array a(n) int * ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void printArray ( int * array , int numberItems )<br />{<br /><br /><br />int i = 0;<br />cout << endl << endl;<br />cout << "array : " << endl;<br />while ( i < numberItems )<br />{<br />cout << array [ i ] << " " ;<br />if ( i != 0 )<br />if ( i % NUMBERS_PER_LINE == 0 )<br />cout << endl;<br />i ++;<br />}<br /><br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : generateNumbers<br />Parameters :<br /><br />numbers a(n) int ,<br />numberItems a(n) int<br /><br /><br />Returns: Void type<br />Comments: Generates an array of random numbers.<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void generateNumbers ( int * numbers , int numberItems )<br />{<br /><br />int i = 0;<br /><br />srand ( time ( NULL ) );<br />while ( i < numberItems )<br />{<br />numbers [ i ] = rand () % MAX_NUMBERS;<br />i ++;<br />}<br /><br /><br />return;<br />}<br /><br /><br />GWILouisaxwzklaParticipantcould do something like this ( I’m not real good with stdio.h , In school I used iostream.h ) :
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758/*Takes currency input from user and outputs currency conversions for USD, GBP, CAD,EUR, AUD, CYN (Chinese Yuan)*/#include <stdio.h>#include <stdlib.h>main(){/* Declare and Initialize Variables */const float USD = 1.0000;const float GBP = 0.5653;const float CAD = 1.0637;const float EUR = 0.7006;const float AUD = 1.2335;const float CYN = 6.8400;const float UserInput = '';/* Begin user interaction *///could use printf ( "%20,-c" , '-' ); for this ( vc++ dosen't seem to like printf options )printf("|<hr class="bbcode_rule" />|n");printf("%10s", "|" );printf( "%8s" , "USD" );printf( " | " );printf( "%8s" , "GRP" );printf( " | " );printf( "%8s" , "CAD" );printf( " | " );printf( "%8s" , "EUR" );printf( " | " );printf( "%8s" , "AUD" );printf( " | " );printf( "%10s" , "CYN |n" );printf( "%10s" , "DOL |" );printf( "%8.2f" , CYN ); //using CYN as every value for exampleprintf( " | " );printf( "%8.2f" , CYN );printf( " | " );printf( "%8.2f" , CYN );printf( " | " );printf( "%8.2f" , CYN );printf( " | " );printf( "%8.2f" , CYN );printf( " | " );printf( "%8.2f" , CYN );printf( "|n" );return 0;}GWILouisaxwzklaParticipantcould do in C++ ( C is similar ):
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124<br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : May 16, 2009<br />* Comments : new project<br />* Compiler/Assembler :<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 12:40:41 a.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br />#include <br />#include <br />//#include <br />//#include <br />//#include <br />//#include <br /><br />using namespace std;<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength );<br />void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength );<br /><br />//##################################################################################<br /><br /><br />//main function ******************************<br /><br />int main ( )<br />{<br />int array [ 10 ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 };<br /><br /><br />writeArrayToFile ( array , "c:\programs\data.txt" , 10 );<br />readArrayFromFile ( array , "c:\programs\data.txt" , 10 );<br /><br /><br /><br />return 0 ;<br />}<br /><br /><br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : readArrayFromFile<br />Parameters :<br /><br />inputArray a(n) int * ( int * ) ,<br />fileName a(n) char * ( char * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void readArrayFromFile ( int * inputArray , char * fileName , int arrayLength )<br />{<br /><br />fstream file ( fileName );<br /><br />if ( file.fail() )<br />{<br />cout << "input file did not open" << endl;<br />return;<br />}<br /><br />int i = 0;<br />while ( i < arrayLength )<br />{<br />file >> inputArray [ i ];<br />i ++;<br />}<br />file.close();<br />return;<br />}<br />/******************************* FUNCTION DEFINITION ******************************<br /><br />Name : writeArrayToFile<br />Parameters :<br /><br />outputArray a(n) int * ( int * ) ,<br />fileName a(n) char * ( char * )<br /><br /><br />Returns: Void type<br />Comments:<br /><br /><br /><br />++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/<br />void writeArrayToFile ( int * outputArray , char * fileName , int arrayLength )<br />{<br /><br />ofstream file ( fileName );<br /><br />if ( file.fail() )<br />{<br />cout << "output file did not open" << endl;<br />return;<br />}<br /><br />int i = 0;<br />while ( i < arrayLength )<br />{<br />file << outputArray [ i ] << " ";<br />i ++;<br />}<br />file.close();<br /><br />return;<br />}<br /><br /><br />GWILouisaxwzklaParticipantIf you want to see if a date is less than another , I would do:
123456789101112131415161718192021222324<br />if ( expirationYear > licenseYear )<br />licenseValid = true;<br />else if ( expirationYear == licenseYear )<br />{<br />if ( expirationMonth > licenseMonth )<br />licenseValid = true;<br />else if ( expirationMonth == licenseMonth )<br />{<br />if ( expirationDay > licenseDay )<br />licenseValid = true;<br />else if ( expirationDay == licenseDay )<br />licenseValid = true;<br />else<br />licenseValid = false;<br />}<br />else<br />licenseValid = false;<br />}<br />else<br />licenseValid = false;<br /><br /><br />GWILouisaxwzklaParticipantIf your original calculations are correct it should be fine. You’ll need to test the program thought…….
GWILouisaxwzklaParticipantyeah , that makes sence :) .
GWILouisaxwzklaParticipantoops. try this version with total as a double :
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889<br />#include<iostream.h><br />#include<conio.h><br />#include <ctype.h><br /><br />float b=1000;<br />void main()<br />{<br />//clrscr();<br /><br />double total = 0;<br />double amount;<br />double temp;<br />char choice;<br />cout << "nwelcome 2 my grocery store!!!!";<br />cout << "nwould u like 2 see the items available for purchase(y/n)? ";<br />cin >> choice;<br />cin.get();<br />if ( choice == 'Y' || choice == 'y' )<br />{<br /><br />do<br />{<br /><br />cout << "the items available are:";<br />cout << "nno."<<'t'<<"item "<<'t'<<"quantity(b)(in grams)"<<'t'<<"m.r.p(c)";<br />cout << "n---"<<'t'<<"---- "<<'t'<<"<hr class="bbcode_rule" />"<<'t'<<"<hr class="bbcode_rule" />";<br />cout << "n1. "<<'t'<<"urad dal "<<'t'<<"1000g "<<'t'<<"Rs.50.25";<br />cout << "n2. "<<'t'<<"tur dal "<<'t'<<"1000g "<<'t'<<"Rs.60.75";<br />cout << "n3. "<<'t'<<"tea powder "<<'t'<<"1000g "<<'t'<<"Rs.200.0";<br />cout << "n enter choice => ";<br />cin >> choice;<br />cin.get();<br /><br /><br />if( choice == '1' )<br />{<br />cout << "nenter the amount of the item u want to purchase ";<br />cin >> amount;<br />temp = ( amount * 50.25 ) / 1000.00;<br />total = temp + total;<br />cout << "n the amount to be paid is:" << temp << "n";<br />}<br />else if( choice == '2' )<br />{<br />cout << "nenter the amount of item ";<br />cin >> amount;<br />temp = ( amount * 60.75 ) / 1000.00;<br />total = temp + total;<br />cout << "nthe amount 2 b paid is:" << total << "n";<br />}<br />else if( choice == '3' )<br />{<br />cout << "nenter the amount of item ";<br />cin >> amount;<br />temp = ( amount * 200.00 ) / 1000.00;<br />total = temp + total;<br />cout << "nthe amount 2 be paid is:" << total << "n";<br />}<br />cout << "ndo u want to buy another item? ";<br />cin >> choice;<br />cin.get();<br />} while ( choice == 'y' || choice == 'Y' );<br /><br />cout<<"nplease wait.... ur bill is being calculated...." << endl;<br />cout << "the total is " << total << endl;<br /><br /><br />cout << "nplease pay the requested amount" << endl;<br />cout << "nthank you.please visit again" << endl;<br /><br />cout<<"nthanks for visiting my store !!!" << endl;<br /><br />}<br />cout << "nthank you.kindly try purchasing the next time u visit the store!!!!!" << endl;<br /><br />getch();<br />}<br /><br /><br /><br /><br /><br /><br /> - AuthorPosts