-
vectors
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 >> "";
}
-
there are some errors here,
Code:
#include <cstring>
#include <vector>
#include <iostream>
#include <cstdlib>
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);
}
};
int main(){ //NO void main, main must return int
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 >> ""; //error here, what are you trying to do here?
return 0;
}
-
vectors
Alpha:
Thanks, but it doesn't work any different when making main an integer type. The difference between void and int is that int must
return a value and void doesn't return a value.
But, you did not answer my main question.
-
>The difference between void and int is that int must return a value and void doesn't return a value.
True, but in the case of main, anything but a return value of int is undefined and quite wrong.
>But, you did not answer my main question.
I will.
>Why does the statement cout<<" numbers[i]=" << numbers[i]; always print out the last number in the vector numbers?
Because you don't initialize vectors like that, remember that vectors are not arrays. There's no constructor that takes a parenthesized list and assigns each value in that list to the vector. The reason you get the last item in the list is because of how the comma operator works. Every expression but the last one will be evaluated and the result discarded, after all of this your constructor looks like this:
vector <int> numbers ( 6, 1437 );
Which assigns 6 copies of 1437 to the vector. Instead, you should create an empty vector and simply push the values onto it or set the size initially:
vector <int> numbers ( 6, 0 );
And place each value manually with the subscript operator:
numbers[0] = 9753;
numbers[1] = 1245987;
...etc
>cout << p->assignNumbers ( numbers, howMany, slots );
You've assigned no memory to p, dereferencing a garbage pointer is undefined.
Try this instead:
Code:
int main(){
int slots=4;
vector <int> numbers;
vector <int> howMany;
numbers.push_back(9753);
numbers.push_back(1245987);
numbers.push_back(4833);
numbers.push_back(34473);
numbers.push_back(8733);
numbers.push_back(1437);
howMany.push_back(5);
howMany.push_back(2);
howMany.push_back(4);
howMany.push_back(3);
howMany.push_back(2);
howMany.push_back(4);
SpeedDial p;
cout << p.assignNumbers(numbers,howMany,slots);
}
-Prelude