Maritimo 15 Junior Poster in Training

Yes, we largely agree!

In my previous post I didn't write the good things about C. Two of them that I can mention are the short time you need to learn it and the easy way to write a compiler for a new machine, neither a depreciable quality. As an old C programmer, I had been in love with C so my feelings can’t be changed easily.

But I want to say that if today I would be forced to return to program in C, it would be very difficult for me. It would be as difficult as forget my cellular and return to the time I must talk throw radio, CP1ADE (Charly Papa One Alpha Delta Eco) was my signal call :-)

I think that the best thing that might happen is the union between the ISO committees of C and C++. But for good or bad, it seems that that will never happen, isn’t it?

I appreciate your good comments. Thank you.

Maritimo 15 Junior Poster in Training

I recomend you to use switch...case clause:

for(int i=1; i<=12; i++)
{
   cout << i << ' ',
   switch(i)
   {
     case 1: cout << "My first message\n"; break;
     case 2: cout << "My second\n"; break;
     //...
   }
}
Maritimo 15 Junior Poster in Training

Maazu: Sure, thanks. I must admit that I don't know how to use other characters than ASCI in a system independent mode. If you can write a post with this in mind I'll appreciate.

Maritimo 15 Junior Poster in Training

Deceptikon: Let me say that C is a very good language. I think it is the second best language that exist. As a matter of fact I have been programming in C a lot of years. However, C++ is a super set of C, so if you know C++ you know C. Also, you can compile "almost" any C program in a C++ compiler.

I think that the best language is C++. C++ is better than C because it has all that C has plus more. So, C++ at least can be as good as C but not less. However, if you need or want to use those other features, C++ is better. In this way any comparison between C and C++ goes in favor of C++.

Sorry if I gave the impression that C is more than just an intermediate step to C++. Probably I made this mistake because this is the way I learned C++. On the contrary, I believe that is better to learn C++ directly instead of learning C first and then C++, that is because the way of thinking in C++ is dramatically different to the way of thinking in C. I believe that it is a mistake to program in C++ “thinking” in C.

I think that C is “close to the machine” while C++, having all that has C, have many other things that sets the language “close to the problem to be solved”. The facilities added to C to create C++, such …

Maritimo 15 Junior Poster in Training

I recomend you to learn C++11 directly. Learning C is the long way to C++98 to C++03 to C++11 and do not have any benefit. As a matter of fact, the style of programming and the way of think has evolved a lot. With C++11 there are many things you must forget from C++03 from C++98 form C. So jump directly to the best.

Note: There are a just new version called C++14 but it is only a very minor upgrade from C++11.

Maritimo 15 Junior Poster in Training

Any comments?

Maritimo 15 Junior Poster in Training

Lilgenski16: As you solved the problem and I think this could be a good example to show another style of programming, I am appending my own version (based on yours).

I think that writting a program is like writting a book, that is, there aren't a fixed number of rules that you must follow. The idea when writting a program is to express what you want in the best way for other humans comprehension.

Based on that, I like to write using the minnimum number of line codes and don´t writting things that are completly unnecesary. In this program, for example, the destructors don't need to be virtual; as a matter of fact, you don't need any destructor at all. So I deleted all the destructors. The default destructors are enough.

I would like to read others comments about the programmings style.

So, my complete alternative, written in C++11 full commented with 295 lines of code is the following:

//==================================================================== Black Jack:
//Plays a simple version of the casino blackjack game with 1 to 7 players.

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>  // srand, random_shuffle
#include <ctime>      // time()

using namespace std;

//-------------------------------------------------------------------- Class Card:
class Card
{
public:
  enum rank {ACE = 1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN,
         JACK, QUEEN, KING};
  enum suit {CLUBS, DIAMONDS, HEARTS, SPADES};

  Card(rank r, suit s): m_Rank(r), m_Suit(s) {}

  int getValue() const // returns the value of a card: 1 - 10
  {
    int value = m_Rank; …
Maritimo 15 Junior Poster in Training

You are making a some mistakes with pointers.

Don't use delete[] elemento but delete elemento
Don't use delete[] prossimo but delete prossimo

You can not write something like delete pun[i]; because pun is not an array.

Declare the variables n and m.

Maritimo 15 Junior Poster in Training

My recomendations are:
You must write the << operators
I don't think is a good idea to Derive Deck from Hand
I recomend you to use the following hierarqui:

  Card n.........1 GroupOfCards
                    |        |
                    |        |
                  Hand      Deck
                    |
                    |
              GenericPlayer (v)
               |         |
               |         |
         (v) Player    House (v)


  Deck 1.....1 Game 1....1 House
                 1
                 .
                 .
                 n
              Players

Make the program more compact, this will help you to understand.
Don't write anything that is not strictly necessary.
Don't use pointers (new, delete).
Use the C++11 new facilities like for(auto& card : m_Card) //...

Maritimo 15 Junior Poster in Training

We don´t do the homework of anybody. Work on your problem and make some questions. We will only give you some hints.

Maritimo 15 Junior Poster in Training

Nathan Oliver, you gave the write answer. My plan was to let some time for others to think about. But thank you. Also your correction about the self assignment is correct. I didn´t put it because I was waiting to be discovered. I am new here and I don´t know if you use to put some problems to solve and learn. I did this because I think it could be nice. Thanks.

Maritimo 15 Junior Poster in Training

Rubberman: I chanched the down-vote. Sorry. Please don´t go into a war.

rubberman commented: I think we understand each other better now. All down-votes are removed, and thanks for the comments. I'm still new with C++11. +12
Maritimo 15 Junior Poster in Training

Interesting point.

I am using C++11. On C++11 you should use { } instead of ( ). This is called the universal initialization. It is not an error.
On C++03 and prior you must use ( ). On C++11 it is up to you to use the universal initialization { } or the old initialization ( ), however the former is recomended.

For this problem please forget this point between parentesis and brackets! The same problem can be formulated with parentesis.

Maritimo 15 Junior Poster in Training

One of my students make me a question (which answer i know) but I think could be instructive to formulate the same question here so many people can learn about it.
The question is:
How to implement a copy assignment into a Derived class?
Consider the following class A:

class A
{
public:
  A(int i1_) : i1{i1_} {}
  A(const A& a) : i1{a.i1} {} // Copy constructor
  A& operator=(const A& a)    // Copy assignment
    {
       i1=a.i1;
       return *this;
    }
private:
  int i1;
};

and you want to implement a derived class B:

class B: public A
{
public:
  B(int i1_, int i2_) : A{i1_}, i2{i2_} {}
  B(const B& b) : A{b}, i2{b.i2} {}   // Copy Constructor
  B& operator=(const B& b)            // Copy Assignment
    {
       /* HOW TO COPY b.i1 INTO i1 HERE? */
       i2=b.i2;
       return *this;
    }
private:
  int i2;
};

Note that in the copy constructor you can write : A(b) to copy b.i1 into the base.
Well, how you can make the same in de copy assignment?
Obviously you can not write i1=b.i1; because i1 is a private variable.

This is a question not for experimented but for medium and novice programmers to think and learn.
Enjoy!

Maritimo 15 Junior Poster in Training

I recomend to you to use vector<int> v and sort(v.begin(), v.end()); all your program can be written in less than 10 lines.

Maritimo 15 Junior Poster in Training

I preffer the following alternative that print an inverted trinagle with any specified number of lines:

include <iostream>
void makeInvTri(int rows, int spaces = 0)
{
  if (rows < 0) return;
  int r = rows;
  int s = spaces;
  while(s--) std::cout << " ";
  while(r--) std::cout << "*";
  std::cout << std::endl;
  makeInvTri(rows - 2, ++spaces);
}

void invTri(int rows)
{
  makeInvTri((rows-1)*2+1);
}

int main()
{
  invTri(1); // Here you can specify any number of lines.
}

With this alternative, the triangle always end in only one star.

In the example you obtain an eleven rows triangle:

*********************
 *******************
  *****************
   ***************
    *************
     ***********
      *********
       *******
        *****
         ***
          *
Maritimo 15 Junior Poster in Training

I recomend you to use a little endian representation of your string numbers, because it is easy to manage the carry. You can try something like:

string add(const string& n1, const string& n2)
{
  // Add two string numbers in little-endian format (reversed)

  const string& min = n1.size()<n2.size() ? n1 : n2;
  const string& max = n1.size()<n2.size() ? n2 : n1;

  int minsize = min.size();
  int maxsize = max.size();

  string res;
  res.reserve(maxsize+1); // This is for optimization purpose only.

  int c=0;
  for(int j=0; j<minsize; ++j)
    {
      int s = c + max[j] + min[j] - 2*'0';
      c = s/10;
      res += '0' + s%10;
    }
  for(int j=minsize; j<maxsize; ++j)
    {
      int s = c + max[j] - '0';
      c = s/10;
      res += '0' + s%10;
    }
  if(c) res += '0' + c;

  return res;
}

To use this, you need to reverse your string numbers before to call add, and after to get the result.

For example, to add 84 with 456 you must call:
string res = add("48", "654"); giving res == 045.
The finel answer is the reverse of 045 which is 540.
That is 84 + 456 = 540.

Maritimo 15 Junior Poster in Training

You can try something like this:

class myIntArray
{
   int* data;
public:
   myIntArray(int size) : data(new int[size]) {}
  ~myIntArray() {delete[] data;}

  int& operator[] (int pos) {return data[pos];}
   //...
};
Maritimo 15 Junior Poster in Training

A function can return a value, for example:
int addOne(int a) {return a+1;}
and be used like:
b = addOne(b);

Also it can not return a value, for example:
void addOne(int& a) {a = a+1;}
and be used like:
addOne(b);

In this case, you have both alternatives and you can choose between them depending your personal taste, the clarity, etc.

Exists other functions that never need to return nothing like:
void printLine(int n) {while(n-->0) std::cout '-'; std::cout << std::endl;}
and be used like:
printLine(20);
to write a Line of 20 - : --------------------.

Maritimo 15 Junior Poster in Training

void* memset( void* dest, int ch, std::size_t count );
Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest.

This means that if your destination can´t be looked as unsigned chars, memset will not work. For example:

#include <iostream>
#include <cstring>

int main()
{
    int a[20];
    std::memset(a, 0, sizeof(a));     /* Note 0 */
    for (int ai : a) std::cout << ai;
}

will work; but:

#include <iostream>
#include <cstring>

int main()
{
    int a[20];
    std::memset(a, 2, sizeof(a));     /* Note 2 */
    for (int ai : a) std::cout << ai;
}

will not!

In conclusion, if you can use memset, it is better because it is faster. The trick is hiden in the words: if you can use. In the case of -1, you can use memset. However, for clarity and if the speed is not an issue, I adhere to the recommendation of deceptikon.

Maritimo 15 Junior Poster in Training

In C/C++ you must define everything befor use it. For example, if you need a variable to store an integer --say a--, first you must define it like:
int a;
and later you can use it like
a=3;.

The same happens, for example, with functions.
Suppose that you have declared a function on one file, say:
int addOne(int a) {return a+1;}.

Later, if you want to use this function in some other file, first you must declare it:
int addOne(int a);.
Then you can use this function like:
b = addOne(b);

If you want to use this function in other file, you must do the same, declare and use:
int addOne(int a); c = addOne(a);

It is impossible to remember all the function's declarations so you can define them before use; also it is very tedious to write all the declarations. This is where the headers file help you.

For this function you can write a file (named addOne.h for example) in where you write the declaration of your function:
int addOne(int a);
When someone needs to use your function, he/she only needs to include this file into his/her file:
#include <addOne.h>
and use the function freely.

In conclusion, the purpose of the header files is to save the declarations of variables, functions, structures, macros, etc; so you can use them later into your program just including these heather files. This makes very easy to declare everything before using it in a systematic way.

The …

Maritimo 15 Junior Poster in Training

I think you are doing a good job. Continue.
I recomend you to change the reverse function with:

void reverse(char line[], int len) {
  int i;
  char tmp;
  --len;
  for(i=0; i<=len/2; i++)
    {
      tmp = line[i];
      line[i] = line[len-i];
      line[len-i]=tmp;
    }
}

To understand it, you must work with a small line of text on a piece of paper as deceptikon said.

Maritimo 15 Junior Poster in Training

You could use something like:

  string s;
  cin >> s;
  for(char c : s) cout << c << ", ";

but you need to solve how to not write the last ',' yet. Also this has the problem to work with letters and numbers. Anyway, this is a start point for your work.

Maritimo 15 Junior Poster in Training

I don´t think that a^n = O(n!).

Supposing that O(x) means the order of magnitude of the number of computer operations you need to compute something, this means that O(2) need 2 computer operations to compute something and O(4) needs 4.

Now suppose that a^n = O(n!) is true, then this means that a^2 needs O(2!) = 2 operations.

That is 2^2 needs 2 operations and 4^2 needs 2 operations also.

Well, accepting that a^n= O(n!), 2^4 should need O(4!)=24 operations; but 2^4 = (2^2)^2.

2^2 need two operations to be computed, giving 4; then 4^2 needs another two operations to give 16.

That is, you computed 2^4 = (2^2)^2 in four operations, not 24.

This means that a^n = O(n!) is false.

If O(n) has another meaning (like order of precision, or so on), the same demonstration shows that a^n = O(n!) is false.

Maritimo 15 Junior Poster in Training

Definitivelly the most powerfull language of all times is...

English.

The most powerfull computer is an...

Engineer.

Explain your problem to an Engineer in English, and he/she will solve it!

Easy, isn't it?

Tcll commented: do you know what a computer is? +0
Maritimo 15 Junior Poster in Training

What is the proble you have with this code?
1 Do not compile?
2 Do not give the expected results?
3 You do not undertand what is does?
4....

Maritimo 15 Junior Poster in Training

You could user something like:

struct Number
{
  union Value {int i; double d;} value;
  enum Type {integer, real, none} type;

  Number(): type{Type::none} {}

  void setvalue(int i)    {value.i = i; type=Type::integer;}
  void setvalue(double d) {value.d = d; type=Type::real;}
};

and later use it like:

 Number n;
  n.setvalue(3);

  if(n.type == Number::Type::integer) cout << "integer: " << n.i << "\n";
Maritimo 15 Junior Poster in Training

You could define the following function:

char invert(char c)
{
  return isupper(c)? tolower(c) : toupper(c);
}

and then use like this:

for(int i=0; input[i] != '\0'; i++) input[i]=invert(input[i]);
Maritimo 15 Junior Poster in Training

You could use something like this:

std::map<char,int> dict;
int c;
while((c = getchar())!=EOF) dict[c]++;
for(char& p : dict) std::cout << p.first << ": " << p.second << std::endl;
Maritimo 15 Junior Poster in Training

I don´t think thos is a C++ problem. It seems an operating system problem. Which is your operating system?

Maritimo 15 Junior Poster in Training

I would want to add the following:

C:
C compiler is written in C.

C++:
C++ compiler is written in C++.

Java:
Sun actually has multiple JVMs. The HotSpot JVM is written largely in C++, because HotSpot is heavily based on the Animorphic Smalltalk VM which is written in C++.

Python:
Python's mainstream implementation is in C.

C#:
C# compiler is written in C++ and in C#

Operating systems like Windows, Linux, Unix, Android, MaxOS:
Are written in C++.

Microsoft:
Literally everything at Microsoft is built using C++.

This probably means that C++ is also a very good language to write operating systems, compilers and interpreters.

Maritimo 15 Junior Poster in Training

Yap, you are wright.
I wrote:

while(k < 2*n-1)

and

double s = f(a) + f(b);

Insted of:

while(k <= 2*n-1)  

and

double s = f(a) - f(b);

Sorry.

Also, in the future i´ll try not to give the complete answer.

Maritimo 15 Junior Poster in Training

Your problem is that

char grades[]

don´t have the required space of memory.

Try something like:

char grades[100];

and be sure that

counter

is alway below 99.

Maritimo 15 Junior Poster in Training

This should work:

#include <iostream>

using namespace std;

int main()
{
  int var;
  cout << "please enter any integer" << endl;

  while(!(cin>>var))
    {
      cin.clear();
      cin.ignore(1000,'\n');
      cout << "Error, enter the right thing" << endl;
    }

  cout << "You entered: " << var << endl;

  return 0;
}

Enjoy :)

Maritimo 15 Junior Poster in Training

The analitical answer is 0.440244, so this is the limit that you must arrive with a lot of intervals.

Here you have your code modified to the right answer:

#include <iostream>
#include <cmath>

using namespace std;

double f(double x)
{
  return ((0.6369)*sin(sin(x))*sin(x));
}

int main()
{
  double a = 0.0;
  double b = 1.5707963267948966;
  int n = 200000;
  double h = (b-a)/(2*n);
  double s = f(a) + f(b);
  int k = 1;
  double x;

  while(k < 2*n-1)
    {
      x = a+h*k;
      s+= 4*f(x)+2*f(x+h);
      k+= 2;
    }

  cout << "integral is equal " << s*h/3;
}

With n=20 I obtain 0.412218
With n=200 I obtain 0.437438
With n=2000 I obtain 0.439964
With n=20000 I obtain 0.440216
With n=200000 I obtain 0.440241

Enjoy :)

Maritimo 15 Junior Poster in Training

This easy code delete multiple occurences of ItemType x from a
and works even for an array plenty of only x:

void SortedList::DeleteItem(ItemType x)
{
    int i=-1, j=0;
    while(j<MAX_ITEMS)
    {
        while(j<MAX_ITEMS && a[j]==x) ++j;
        if(j<MAX_ITEMS) a[++i]=a[j++];
    }
    MAX_ITEMS=i+1;
}

Enjoy :)