Pause it.
if (lower_first > upper_first)
{
printf("Lower-bound IP address > upper-bound IP address");
cin.get();
exit(0);
}
Pause it.
if (lower_first > upper_first)
{
printf("Lower-bound IP address > upper-bound IP address");
cin.get();
exit(0);
}
No its not valid. And thats because of the operator precedence.
Here is an example that shows how to make it valid :
class Int{
public :
int var;
Int() { var = 0; }
Int(const int& i) { var = i; }
};
int main(){
Int * num = new Int(0);
//how to access num data
(*num).var = 1; //option 1
num->var = 1; //option 2
delete num;
}
The operator -> is a special operator and is made for the sole reason
to make the syntax better. Rather than doing (*num).var = 0, doing
num->var = 0 is the same thing.
Lets take test cases an see where that leads us.
<input> 4 2
<output> 4/2
We need the output to show it in the reduced term so 4/2 reduces to
2/1 which reduces to 2.
For the test case above name some things you notice about 4/2 ?
This is wrong :
T min(vector<T> a())
Try to guess which part.
Edit : Hint look at the error
A class :
class Test{};
A pointer :
int *p = 0;
Combine them and you get a "class pointer" :
class Test{}; Test *p = 0;
clock() returns the time in mili seconds, CLOCKS_PER_SEC is the conversion
factor from the value returned by clock() into seconds.
while (clock() < stopwait) {}
That code waits until the value returned by clock() is greater than
stopwait, meaning, it waits X amount of milliseconds, where x is stopwait.
You need to make it a reference.
In fact do something like this :
void write(const string& str, std::ostream& out = cout){
out << str << endl;
}
Now you can call the code like this :
write("Hello"); // and prints to screen
write("Hello",myout); //and write to file
You might be looking for constructor with default value :
struct Int{
int var;
Int(const int& initValue = 0) { var = initValue; }
};
Therefore when you do this :
Int num = Int(); //num = 0 for now
Int num2 = Int(2); // num = 2 for now
1) Rename everything to something appropriate.
2) Don't worry about speed right now
3) Make function to read and write to a file
4) Remove all try and catch statements
5) Terminate process if an error occurs
6) Use std::string
Fix those and get back.
When using list, you have to use iterators. When using vectors you
have a choice of using index or iterators, usually you should pick the index
version( which means use the operator [] ). The STL uses iterators
to generalize their functions. For example using std::sort(..), it takes in
a begin and a end iterator. That means that the algorithm stl uses does not have
make different algorithms for different containers.
That code is pretty bad. Its congested with unneeded try and catch
statement. You naming convention is very bad. You code is not easy on
the eyes. You are using a char arrays which is a bug in my book.
Why open it in binary? You do not comment on your code.
You do not make use of functions to make code more readable.
Prefer getline(...) over istream.getline().
Overall I would burn that code, sorry and give you a C.
P.S : You spelled practice wrong in your tag.
i have a numeric updown
that goes from 0-99 and it works but i want to make it continuous
so if i hit the down when its at 0 it goes to 99 and when its on 99 and i hit up it goes to zero
anyone know the code id add to make that happen?
You need to be more specific, are you using only C++?
Is this what you are trying to achieve( in psuedo-code) ?
long num = 0;
while true{
print : Press up or down;
char keyPressed equals getKeyPressed();
if keyPressed is up the print : ++num;
else print : --num;
}
Oh when you say pair you mean in an 2d array like structure?
Like so :
Random generate= new Random();
int[][] Array2d = new int[4][4];
for (int row = 0; row < Array2d.length; row++){
for (int col = 0; col < Array2d[row].length; col++){
Array2d[row][col] = generate.nextInt(8) + 1; // [0,8)
}
System.out.print("\n");
}
So the above code might print out :
1 2 5 1
5 1 2 3
4 6 1 7
1 3 1 2
Here are some hints :
For the divelement function you need to see if a number is multiple
by 5? One way to do this is to use the mod operator. The mod operator returns the remainder. Here is an example on how to use it :
//returns true if a number if multiple of 7
bool isMultipleOfSeven(int num){
return num % 7 == 0;
}
The main part is num % 7 == 0. For example say the input is 14.
Then 14 div 7 = 2 with 0 remainder. So if a remainer is 0 then 14 can
be evenly be divided by 7. Lets take a look at another example,
12 div 5 = 2 with 2 remainder. To see why look below :
_2__
5| 12
- 10
-----
2
We see that, 5 goes into 12, two times, and we have 2 remainder. Thus
because the remainder is not 0, 12 is not a multiple of 5.
Now for the search function, your prototype is wrong, it should be this:
.
//Array[] is the array in which we will search in
//size is the maximum size of the array
// searchKey is the value we are looking for
int search(int Array[], int size, int searchKey);
Now for the search function all you have to do is loop over the whole
array, while checking if the searchKey is there.
A programmer will grow more with time from work experience... so it doesn't always the criteria is to hire super smart programmers. the important quality there is potential.
Sure potential is an important quality, but would you hire a dull person
to make a software for you company, even if that dull person has
potential? Or would you hire a "*smart" person for that same job?
Remember its you ass on the line.
------------------------------------------------------------------------------------------
* whatever smart means these days.
Well try it on paper. Draw picture and see what you have to do. Then
report back.
Your code is really hard to look at. But from your question : "cant
determine the winner" I figure that the winner checker is not working.
There are 2 things you can do.
Option1 : Make a hard-coded function that checks for winner
Option2 : create a for loop to that checks the winner.
I would say Option1 is easier. So For now, you should get that working.
To check for winner you need to first create a function like so :
bool checkForWinner(const char *Board[3], const char key){
if( Board[0][0] == key && Board[0][1] == key && Board[0][2] == key)
return true;
//and so one
}
Thats the hard-coded way. Try and see if you can get that working.
Also try to make your code neater and easier to read.
combine your even and odd in one loop :
for(int num = start; num != end; ++num){
if(num % 2 == 0) cout << num << " is even\n";
else cout << num << "is odd\n";
}
so you need a function of this sort :
//char *Array is the array that will be inserted with value
// const int Size is the maximum size of the Array
//const int where is the index where the char value will be inserted
//const char value is the actual value that will be inserted
void insert(char *Array, const int Size,const int where, const char value){
//logic goes here
}
I am not sure whats your problem? Do you want to generate random
character, if so then use something like this :
//returns a "random" letter
char getRandomChar(){
return rand() % ('z' - 'a') + 'a'
}
>Averagely speaking, I doubt that a person without skills at say
>playing basketball, would put hard work and dedication to make
>it to the NBA
The NBA is very much a young person's arena. Of course you need talent to reach that level before age starts to take effect. I assume you brought up basketball because professional piano playing and programming don't have that particular restriction, and your argument would fall flat without a straw man. ;)
True that there are different restrictions, but it still takes the same
hard work and dedication to reach the top level, unless you have
special privileges. And my main argument was that your theory only sounds good on paper,
but in practice, it rarely happens.
>To be good at playing the piano takes real talent and a special mind.
I disagree. Hard work can overcome lack of talent. While a talented pianist can get to the stage faster, an untalented pianist can still get there. The same goes for programming.
Averagely speaking, I doubt that a person without skills at say
playing basketball, would put hard work and dedication to make it
to the NBA. Same for programming, I doubt that a person with no
programming talent will have the ambition and the dedication to
become a professional( and actually go through it). In theory, the
dedication over talent argument, sounds plausible, but in practice,
people rarely follow through.
That sucks so bad. If it makes you feel better, my classes this semester
is a tough load. Its busting my chops and we just started.
This code causes a memory leak.
IntSLLNode *tmp;
tmp = (IntSLLNode*)malloc(sizeof(IntSLLNode));
tmp = head;
So does this code :
IntSLLNode *oldTmp;
oldTmp = (IntSLLNode*)malloc(sizeof(IntSLLNode));
oldTmp = tmp;
>>1. You emphasized on using const wherever I can, I'll but may I know why it is good?
Because making things const not only enables the compiler
to maybe optimize things, it also promises that the variable
wont be altered, and in general, if its not needed to be altered, then make it const.
>>2. If I use parameterless constructor like as u said Matrix(), how'll I initialize dimensions? I am not saying only have Matrix() constructor, have multiple overloaded constructor.
Matrix a ; //default constructor Matrix()
Matrix b = Matrix(3,3); // Matrix(const int& Row, const int& Col);
Matrix c = Matrix(3,3 0 ); //Matrix(const int& Row, const int& Col, const int& initValue);
a = Matrix(2,4); // init Matrix a.
>>3. Can you give me a little more hint on that private function, to be used in constructor?
Make a private function like the following pseudo function:
void _arithmeticHelper(const Matrix& other, const int& Operator){
for i = 0 to Row
for j = 0 to Col
switch( Operator){
case ADD : _myMatrix[i][j] + other._myMatrix[i][j]; break;
case SUBTRACT : ... break;
case MULTIPLY : ... break;
}
}
Where ADD, SUBTRACT, and MULTIPLY is some private static const,
maybe declared like so :
class Matrix{
//private helpers
private:
static const int ADD = '+';
static const int SUBTRACT = '-';
static const int MULTIPLY = '*';
}
>>4. const-corrected means using const parameteres in functions?
There is more to it, look …
I have commented on your code with code comments , see below :
#include<iostream>
using namespace std;
// A TEMPLATE CLASS WILL BE BETTER
class matrix //USUALLY A CLASS NAME SHOULD START WITH A CAPITAL LETTER( from convention)
{
//is the user able to change these ? if not then make it constant
int dim1,dim2; // a better name would be Row and Column
int **mat;
public:
matrix(int x=2,int y=2) //No need for default ctor of that form. Either Matrix() or Matrix(const int
//MaxRow, const int MAXCOL)
{
dim1=x;
dim2=y;
//make a private function that does this, which will make you code neater
mat=new int*[dim1];
for(int i=0;i<dim1;i++)
mat[i]=new int[dim2];
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
mat[i][j]=0;
}
int returndim1() //whats dim1 the Row or the column ?
{
return dim1;
}
int returndim2() //goto : returndim1 comment
{
return dim2;
}
//why ?Just let the user handle this
void input()
{
cout<<"Enter the elements of matrix - ";
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
cin>>mat[i][j];
}
//out is a bad name, and what if user wan't a specific format ?
void out()
{
for(int i=0;i<dim1;i++)
{
cout<<"\n";
for(int j=0;j<dim2;j++)
cout<<mat[i][j]<<" ";
}
}
//use const matrix x, also make this function const-corrected( look it up)
matrix operator+(matrix x)
{
matrix c(dim1,dim2);
if(dim1==x.returndim1() && dim2==x.returndim2())
{
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
c.mat[i][j]=this->mat[i][j]+x.mat[i][j];
return c;
}
else
{
cout<<"Matrix cant be added";
return c;
}
}
//goto comment on operator+
matrix operator-(matrix x)
{
matrix c(dim1,dim2);
if(dim1==x.returndim1() && dim2==x.returndim2())
{
for(int i=0;i<dim1;i++)
for(int j=0;j<dim2;j++)
c.mat[i][j]=this->mat[i][j]-x.mat[i][j];
return …
*sigh*, Another post without code-tags. If only I had $1000 for every
post without code-tags.
You should be using arrays to get the input first, instead of the ugly hard-coding, and use std::strings like so :
const int MAX_INPUT = 10;
string userInputs[MAX_INPUT] = { string(""); }
for (pos = 0; pos < MAX_INPUT; ++pos)
cin >> userInput[pos];
Now make a function called sortUserInputs that takes in an array
of strings, like so :
void sortUserInputs(string *inputs, const int MAX_INPUTS){
//sorting algo goes here
}
Make those changes, try to sort the user inputs, for example you can
use bubble sort, or selection sort. Then report back if problems occur.
i want diz for my final project...tq
Really? I mean really ? I am utterly speechless. Really, you have got to be kidding?
Why do you need pointers?
Now just do something with the gauss function, like so :
double gauss(fun f, double a, double b, int n){
return ( (*f)(a) + (*f)(b) ) * n;
}
The above is just a random example.
Or just create a function that converts to necessary data-types.
hello???
can you give me a complete program about a fraction calculator???
I'll give it to you for 1 zillion gazillion quintillion dollars?
>> i am not dating but once you are into something real, you dont feel like you need females any more..
Sounds more If a hot girl hit on me, then I would be dating, but since I
do not have the courage to hit on hot girls, I found a hobby to keep me
occupied. :)
When posting code, the format should look something like this( see below), if it isn't then make it until it is. This is not only help us read
better, but it will also help you get better comments.
//notice not .h
#include <iostream>
#include <cmath>
using namespace std;
int main ()
{
//initialize variable upon decelerations, why aren't you using arrays?
int n(0),j(0),q(0),d(0),h(0),t(0),i(0),m(0),f(0),b(0);
cout<<" num of eng";
cin >> j;
for (i=1;i<=j;i++)
{
cout<< "insert Salary ";
cin>> m;
//you got is in opposite order, its d = m/22
m /(22) =d;//See what VernonDozier said
d/(8) = h;//See what VernonDozier said
cout<<"\n Leave ";
cin>>f;
cout<< "\n Attend ";
cin >> b;
f - b = t;
t * h = q;
cout<<"q="<<q;
}
return 0; //main returns something
}
Now look back at your 1st post and look at this post, realize how
much more readable this post is?
All you have to do is read the file but insert the elements you get into
its correct place. Is this enough of a hint?
They are the member variables. For example :
class Person{
private:
//attributes
int health;
int power;
public:
Person(){ ... };
}
So a health and strength is an attribute of a person.
Is this not giving you an error :
"stream << hex << number;" ?
Make it stream <<std::hex << number; "
>> outputs a bunch of numbers
What what do you mean? In hex, just numbers could represent
some decimal numbers as well.
Is this what you need :
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
typedef size_t Type;
string toHex(const Type& number){
stringstream stream;
stream << hex << number;
return stream.str();
}
int main(int argc, char *argv[])
{
string num = toHex(15);
cout << num << endl;
return 0;
}
This function :
float readFloat(std::ifstream& readFile)
{
float holdData = 0;
while(holdData != (-(holdData)))//error checking
{
readFile >> holdData;
return holdData;
}
}
Is very bad, especially this part:
while(holdData != (-(holdData)))//error checking
That reads as, while holdData is not equal to negative holdData,
which is always true, unless holdData equals 0. Make your function
similar to this :
float readFloat(std::istream& readFrom){
float result = 0.0f;
if(! ( istream >> result ) ) //if reading failed{
cout << "Error occured during reading, return 0.0f" << endl;
return 0.0f;
}
return result; //else reading was successful
}
>> I'm trying to copy a value in a vector to a string. The vector is also of string type, I'm trying to copy through the iterator
So you are trying to copy the strings inside a vector into an array ? or into
another vector ? or into another string variable.
First declare a function like so :
int readInt(std::ifstream& readFile){
int res = 0;
//error checking if you want
readFile >> res;
return res;
}
Then you can make another function that loops that function like so :
void readIntArray(std::ifstream& readFile, int *Array, const int Size){
int index = 0;
//while index is not equal to Size AND !readFile.fail
// Array[index] equals readInt(readFile);
// ++index;
}
Try something like that.
Usually my password, is something like this , xxxxxxYZZZZ, where
x is a letter, Y is a punctuation, and Z is a number.
Is this the get method you are talking about :
string LinkedList::get()
{
temp1=head;
while( temp1!=NULL ){
cout<< temp1->data<<" ";// show the data in the linked list
temp1 = temp1->next; // tranfer the address of 'temp->next' to 'temp'
}
}
There is a problem with that. You declared it as returning a string,
and you don't return anything. And why is it called get? Call
it printAll() or something since thats what its doing.
After the applicant passing some test, do you ever try to throw
some tricky question, to see if he can answer them, and see how deep is his knowledge on that certain topic( in a sense)? Also is your applicant C, C++ or does not really matter?
Just curious, are there some filtering process that on a normal interview
you might try. For example you might say, write a code to print prime number from 1 to 100, and see his reaction to that statement rather than the code itself. If its a private thing to reveal, then its ok not to.
Usually C++ test are useless. A test just shows that you know definition( in a sense). It does not really show how well you program. Instead of looking for a test, how about you program a big project, with big being
subjective. Learn a new skill. Do you know anything about AI? Do you
know anything of templates and the crazy techniques one can do?
Bottom line, forget about a C++ test and go program some new things
to develop your understanding in a subject that is new to you, or enhance by creating better code on a subject you already know.
Just curious, are you creating a 2d and 3d vector class.
Fair enough. You have to downcast the derived to base class.
e = dynamic_cast<Base&>(d) * 1.0;
> (I loop it 250 times)
...
> srand(unsigned(time(NULL)));
Unless your code also takes 250 seconds to run, then srand() is always going to be seeded with the SAME value, and rand() is always going to return the SAME value(s) as a result.srand() needs to be called ONCE only, at the start of main.
Although I think it could be called before main.