Below is my code.
Why does the statement cout<<" numbers[i]=" << numbers[i];
always print out the last number in the vector numbers? Plus if
you see any things I could be doing better in the code, please let me know.
Code:#include <string> #include <vector> #include <iostream> #include <stdlib> using namespace std; class SpeedDial { public: int assignNumbers(vector <int> numbers, vector <int> howMany, int slots) { int leastKeyPresses=-1; int numbs=howMany.size(); int ttlKeyPrsz=0,ttlKeyPrszSvd=0,copy=0; char snumb[50]; vector <int> noKeyPress(numbs); vector <int> noNumbs(numbs); vector <int> KeyPreszSvd(numbs); for(int i=0; i<numbs; i++){ cout<<" numbers[i]=" << numbers[i]; itoa(numbers[i],snumb,10); noNumbs[i]=strlen(snumb); noKeyPress[i]=noNumbs[i]*howMany[i]; ttlKeyPrsz += noKeyPress[i]; KeyPreszSvd[i]=noKeyPress[i]-(howMany[i]*2); } for(int j=1; j<numbs; j++){ if(KeyPreszSvd[j]>KeyPreszSvd[j-1]){ copy=KeyPreszSvd[j-1]; KeyPreszSvd[j-1]=KeyPreszSvd[j]; KeyPreszSvd[j]=copy; } } if(slots>numbs)slots=numbs; for(int i=0; i<slots; i++){ // cout<<KeyPreszSvd[i]; ttlKeyPrszSvd+=KeyPreszSvd[i]; } cout<<ttlKeyPrsz << " " << ttlKeyPrszSvd << " "; leastKeyPresses=ttlKeyPrsz-ttlKeyPrszSvd; return (leastKeyPresses); } }; void main(){ int slots=4; // int slots=5; /* vector <int> numbers(6,3); vector <int> howMany(1,7); */ vector <int> numbers(6,(9753,1245987,4833,34473,8733,1437)); vector <int> howMany(6,(5,2,4,3,2,4)); /* vector <int> numbers(3,(124839,9876,43823)); vector <int> howMany(3,(73,95,102)); */ SpeedDial *p; cout << p->assignNumbers(numbers,howMany,slots); cin >> ""; }



LinkBack URL
About LinkBacks


