Hello everyone,

If i need my program to create as much vectors as needed based on the user first input , assuming each vectors will hold an int value like a combinations of numbers, so latter those vectors will be compared for higher value.

EX:
prompt the user to enter how many vectors needed for this program ,
3

now user will enter their first vectors value followed by second , and third.
23456
12340
98400

I was thinking of having a loop, but not sure yet how should I create those different dynamic vectors variables in a way to distinguish each one from the others so I can pass them differently into my other methods for comparison and calculation.
I'm new to vectors that's why this might seems silly for some ppl but its little confusing for me.

Thank you.

Dani AI

Generated

OP wanted a runtime number of "vectors" (one per entered combination) and a way to compare them later. ’s pointer idea works, but raw new[]/delete[] adds complexity and leak risk. A clearer, safer pattern in modern C++ is to use std::vector (either a flat vector of integers if each value is a single number, or a vector-of-vectors if each entry should be treated as a sequence of digits).

The common choices:

  • If each entry is just a numeric value that fits in 64 bits, store them in std::vector<long long> and use std::max_element to find the largest.
  • If each entry must preserve digit structure or leading zeros or may be arbitrarily long, read as a string and store digits in std::vector<std::vector<int>>. Comparing then uses length first, then lexicographic digit comparison.

Example: read N strings, store each as a vector of digits, and pick the largest (comparison by length then digit):

#include <iostream>
#include <vector>
#include <string>

bool greaterDigits(const std::vector<int>& a, const std::vector<int>& b) {
    if (a.size() != b.size()) return a.size() > b.size();
    for (size_t i = 0; i < a.size(); ++i)
        if (a[i] != b[i]) return a[i] > b[i];
    return false;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<std::vector<int>> numbers(n);
    for (int i = 0; i < n; ++i) {
        std::string s; std::cin >> s;
        numbers[i].reserve(s.size());
        for (char c : s) numbers[i].push_back(c - '0');
    }
    int maxIdx = 0;
    for (int i = 1; i < n; ++i)
        if (greaterDigits(numbers[i], numbers[maxIdx])) maxIdx = i;
    std::cout << "Index of largest: " << maxIdx << '\n';
}

Notes and tips: pass containers to functions by const reference (e.g. void f(const std::vector<std::vector<int>>& v)), use reserve when sizes are known, and prefer std::vector over manual new/delete for safety. If numeric range is unknown and performance matters, consider big-integer libraries; otherwise string-length + lexicographic comparison is sufficient.

Recommended Answers

All 4 Replies

do you know programming?

Yes sir , i sure do.
do you know programing ???
any other comment related to my question are appreciate it.
thanks.

I don't quite understand why you need to store each int in a array. Wouldn't it be simpler to have a single array of int's? Anyway, if you want it like that you can use a pointer to a array of int arrays. Then you can use each array by the index. Like below:

int nrOfVectors;
std::cout << "Enter the number of vectors: ";
std::cin >> nrOfVectors;
int** n = new int* [nrOfVectors];
for (int x = 0; x < nrOfVectors; ++x)
{
    // here you could also input each array size instead of 5
    n[x] = new int[5];
    for(int j = 0; j < 5; ++j)
    {
        // enter each digit of the int in n[x]
    }
}

// now you can simply use n[0] or n[1] or whatever array you want.

But in the end I still say it's simpler to use a single array of int's.

Thank you so much lon 1.
its a requirement for the assignment that i'm working on. i see the concept and that's all matter. true, i know it can get ugly especially when i start calculating and passing values through other methods.
thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.