Ask Programiz - Human Support for Coding Beginners

Explore questions from fellow beginners answered by our human experts. Have some doubts about coding fundamentals? Enroll in our course and get personalized help right within your lessons.

  • All
  • Python
  • C
  • Java
  • CPP
  • SQL
  • JS
  • HTML
  • CSS
Kelish Rai
Expert
last year
Kelish Rai answered

The main difference between arrays and vectors is that an array is a fixed-size collection of elements, whereas a vector can grow or shrink as needed.

Both are used to store multiple values, but they work a bit differently.

With arrays, once you define the size, it can't be changed. For example:

int arr[5];

Here, you've got space for exactly 5 integers—no more, no less. If you later want to store 6 or more values, you'll need to create a new array and manage copying manually.

Vectors, on the other hand, are part of the C++ Standard Library and are more flexible. You can start with an empty vector and keep adding elements using push_back():

#include 
using namespace std;

vector nums;    // Starts empty
nums.push_back(10);    // Adds 10 to the vector
nums.push_back(20);    // Adds 20

The vector automatically resizes itself in the background as you add elements—no need to specify the size in advance.

Note: If you're working with a known, fixed number of values and performance is critical, arrays can be slightly faster. But in most cases, vectors are preferred for their ease of use and flexibility.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

Yes, you can technically use void main() instead of int main(). However, it's not recommended.

According to the C++ standard, the correct and portable way to define the main function is:

int main() {
    // your code here
    return 0;
}

This version is universally accepted and ensures that your program can properly communicate with the operating system. The return 0; statement signals that the program finished successfully.

If you use void main(), you won't be able to use return 0; because the function doesn’t return anything. That might not cause an error in some compilers, but it's considered non-standard and could lead to unpredictable behavior or reduced portability.

In short:

  • Use int main() for standard, portable, and reliable C++ code.

  • void main() might work in some environments, but it’s not guaranteed.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

In the code:

#include 
using namespace std;

int main() {

    // create a variable
    double number = 83.13;

    // create a pointer variable
    double* pt;

    // assign address to pointer
    pt = &number;
  
    // print pointer pt
    cout << pt;
  
    return 0;
}

The reason the pointer needs to be a double* is that the variable number is of type double.

In C++, the type of a pointer should always match the type of the variable it points to. So if number were an int, then the pointer should also be an int*.

Here's a quick comparison to help make it clearer:

int a = 5;
int* ptr1 = &a;    // OK: both are int

double b = 6.2;
double* ptr2 = &b;    // OK: both are double

double* wrongPtr = &a;    // Not OK: types don’t match (int vs double)

Using the correct pointer type ensures that the program knows how much memory to access and interpret properly. For instance, a double typically uses more bytes than an int, and a mismatch can lead to unexpected behavior.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

I agree that all the names listed in the good and bad variable name examples are valid options for C++ programs.

However, when defining good variable names, the goal is to choose names that are both easy to understand and easy to work with.

For example, it’s much clearer to understand a variable named salary than one named s. Similarly, age is much easier to use and comprehend than something like aGekv2, which doesn’t really convey any meaning.

In short, good variable names should be straightforward, intuitive, and consistent.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

That's alright. Let me break it down simply.

When writing programs, we often need to take input from the user. For example, if we want to ask for a name and then greet the user, we need a way to get that input.

In C++, we use cin to take input and store it in a variable. Here’s how it works:

#include 
using namespace std;

int main() {
    string name;

    cout << "Enter your name: ";

    // Take user's name as input
    // Store user's name in name variable
    cin >> name;

    // Greet user
    cout << "Hello, " << name;

    return 0;
}

If you run this program, you'll get this as output:

Enter your name: 

Now, you need to click on the output and enter your name. Suppose you enter John:

Enter you name: John

Now, cin >> name; stores "John" in the name variable.

Finally, cout << "Hello, " << name; prints:

Hello, John

The thing to note here is that you need to use >> when using cin and << when using cout. Otherwise, you may get an error.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

Here’s the C code for creating nodes in a linked list:

#include 
#include 

typedef struct Node {
    int data;
    struct Node* next;
} Node;

int main() {
    // Create nodes and initialize them
    Node* node1 = (Node*)malloc(sizeof(Node));
    node1->data = 11;
    node1->next = NULL;

    Node* node2 = (Node*)malloc(sizeof(Node));
    node2->data = 2;
    node2->next = NULL;

    Node* node3 = (Node*)malloc(sizeof(Node));
    node3->data = 88;
    node3->next = NULL;

    // Free allocated memory
    free(node1);
    free(node2);
    free(node3);

    return 0;
}

Here,

  • malloc() allows us to allocate memory dynamically.

  • We manually assign values to data for each node.

  • next is initialized to NULL.

  • Before using the pointers, we check if malloc() successfully allocated their memory.

  • free() is used at the end to deallocate memory and prevent memory leaks.

C++
This question was asked as part of the DSA with C++ course.
Kelish Rai
Expert
last year
Kelish Rai answered

To open a file by asking the user for the filename, you simply take their input before attempting to open the file. Here's how you can do it:

#include 
#include 

using namespace std;

int main() {
    string filename;
    cout << "Enter the filename: ";
    cin >> filename;

    // Create an fstream object
    fstream fs;

    // Open the file in read mode
    fs.open(filename);

    // Check if the file was opened successfully
    if (!fs) {
        cout << "Could not open the file." << endl;
        return 1;
    }

    return 0;
}

Output

Enter the filename: myfile.txt  
File opened successfully.

In the code:

  • cin >> filename; takes the filename input from the user.

  • fs.open(filename); attempts to open the file.

  • We check if (!fs) to confirm if the file opened correctly. If it fails, we print an error message and exit the program.

  • At the end, we safely close the file using fs.close();, which is a good practice even if the program ends immediately after.

Note: If the filename contains spaces (like "my file.txt"), cin >> filename; will not work properly because cin stops reading at the first space. In that case, you should use getline(cin, filename); instead.

C++
This question was asked as part of the Learn C++ Intermediate course.
Dejoy Osei Amankwaah
last year
Dejoycountry asked
Palistha Singh
Expert
last year

endl in C++ stands for "end line". It's used to move the output to the next line, just like pressing Enter on the keyboard.

For example:

cout << number1 << endl;

This prints the value of number1, then moves the cursor to the next line before printing anything else.

We can clearly see this in the practice exercise:

int number1 = 89;
int number2 = 313.78;

cout << number1 << endl;
cout << number2;

Output with endl:

89
313.78

Without endl (if we wrote cout << number1;):

89313.78

So endl helps keep output clean and readable by separating it into lines.

C++
This question was asked as part of the Learn C++ Basics course.
Haoran Shan
last year
Haorancountry asked
Kelish Rai
Expert
last year
Kelish Rai answered

In C++, the return 0; statement simply indicates that the program has completed successfully. It tells the system that the program ran without errors.

For example:

#include 
using namespace std;

int main() {
    cout << "Hello, world!" << endl;
    return 0;
}

In the code:

  • The program prints "Hello, world!".

  • The return 0; line sends a status code back to the system—0 traditionally means success.

  • If something went wrong in your program and you wanted to signal that to the system, you could return a non-zero value like return 1;.

Why does this matter?

If you're running your program in a larger system or calling it from a script, the return value lets that system know whether the program succeeded. This is useful in automated testing, system monitoring, or when chaining commands in shell scripts.

So while it may seem small, return 0; plays an important role in communicating the outcome of your program to the system.

C++
This question was asked as part of the Learn C++ Basics course.
Kelish Rai
Expert
last year
Kelish Rai answered

The main difference between function overloading and function overriding in C++ comes down to where and how the functions are defined.

1. Function overloading

  • Happens in the same class

  • Functions have the same name but different parameters (different number or types)

  • The compiler decides which function to call based on the arguments

Example:

#include 
using namespace std;

class Animal {
public:
    // Function with no parameters
    void make_sound() {
        cout << "Animal Sound" << endl;
    }

    // Overloaded function with an integer parameter
    void make_sound(int count) {
        cout << "Animal Sound (" << count << ") times" << endl;
    }
};

int main() {

    Animal a1;

    // Calls the function without parameters
    a1.make_sound();    // Output: Animal Sound

    // Calls the overloaded function with an integer argument
    a1.make_sound(10);    // Output: Animal Sound (10 times)

    return 0;
}

Here, calling make_sound() and make_sound(10) will call different versions of the function.

2. Function overriding

  • Happens between a base class and a derived class

  • Functions have the same name and parameters

  • The derived class function replaces the base class function when called via an object of the derived class

Example:

#include 
using namespace std;

class Animal {
public:
    // Function to make a generic animal sound
    void make_sound() {
        cout << "Animal Sound" << endl;
    }
};

class Dog: public Animal {
public:
    // Overrides the Animal class' make_sound() function
    void make_sound() {
        cout << "Woof Woof" << endl;
    }
};

int main() {

    // Create object of the child (Dog) class
    Dog dog1;

    // Calls the overridden function in child (Dog) class
    // and not base (Animal) class
    dog1.make_sound();

    return 0;
}

// Output: Woof Woof

Here, calling make_sound() on the Dog class executes the function of the Dog class, overriding the make_sound() function of the Animal class.

Also, one thing to note is just because function overloading happens within the same class doesn't mean it can't be used alongside inheritance.

You can still overload functions in a base class and its derived class, as long as the overloaded functions follow the usual rules—having the same name but different parameter lists.

C++
This question was asked as part of the Learn C++ OOP course.