If I want to print a single dimensional array with n number of elements. Can I initialize the array as array[n] ?
#include "pch.h"
#include <iostream>
using namespace std;
int main()
{
int n;
std::cout << "Please enter the number of elements (n): ";
std::cin >> n;
int array[n];
for (int i = 0; i <= n; i++) {
std::cin >> array[n];
}
return 0;
}
As C++ does not support Variable Length Arrays (VLAs) contrary to C99, you'll have to use some other means of allocating memory of arbitrary size in C++ like std::vector:
#include <iostream>
int main()
{
int n;
std::cout << "Please enter the number of elements (n): ";
std::cin >> n;
std::vector<int> foo(n);
// valid indexes range form 0 to size - 1: < n instead of <= n
for (int i = 0; i < n; ++i)
std::cin >> foo[i];
}
Also you mixed up i and n in your for-loop.
std::cin >> array[n] << " ";
^^^^^^
won't work either.
In C++ you cannot initialize an array with a variable length. Either you:
dynamically allocate memory
int *array = new int[n];
in which case you should not forget to deallocate later with
delete[] array;
Or you can use a std::vector
std::vector<int> array(n);
which will be deallocated when it exits the scope.
Additional mistakes are:
The for loop should be like
for (int i = 0; i < n; i++)
because with n elements the array indexes go from 0 to n - 1.
To read the input you can simply use
std::cin >> array[n]
The code you wrote with a combination of >> and << cannot work.
Related
Could someone explain why pointers gets overwritten when variables are declared inside a loop?
For example, given the following snippet, and the user inputs 1 and 2. I would expect that the pNums array contain 2 pointers to 2 integers holding the value 1 and 2 respectively.
But instead, the console prints out 2 and 2;
#include <iostream>
using namespace std;
//Input "1 2"
int main() {
int* pNums[2];
for(int i = 0; i < 2; i++){
int num;
cin >> num;
pNums[i] = (&num);
}
cout << (*pNums[0]) << endl;
cout << (*pNums[1]) << endl;
}
Why is this the case? And how do I get around it? What if, for example, we don't know how many numbers the user will put in, and instead of a for loop, we have a while loop? Until some conditions are met, we want to keep creating new pointers and store them into a pNums vector?
There is only one num, and you are overwriting that. (And then causing Undefined Behavior, but never mind that.)
There are two simple ways to avoid this mistake.
1) Store objects, not pointers:
int nums[2];
for(int i = 0; i < 2; i++){
cin >> nums[i];
}
2) Use dynamic allocation:
int* pNums[2];
for(int i = 0; i < 2; i++){
int *p=new int;
cin >> *p;
pNums[i] = p;
}
The pointers that you are storing in pNums are to two instances of the variable num in the for block. There is one instance of the variable in each for loop iteration and these variables live only until the end of their respective iteration of the for loop body is reached.
Therefore your pointers will be invalid when the for loop exits and so trying to dereference them with e.g. *pNums[0] causes undefined behavior.
Don't store pointer, store values:
#include <iostream>
using namespace std;
//Input "1 2"
int main() {
int pNums[2];
for(int i = 0; i < 2; i++){
int num;
cin >> num;
pNums[i] = num;
}
cout << pNums[0] << endl;
cout << pNums[1] << endl;
}
and if you need a variable number of entries in the array, use std::vector.
for(int i = 0; i < 2; i++){
int num; //< this is num. It lives here.
cin >> num;
pNums[i] = (&num); //< you have taken the address of num (twice!)
}
// here is when 'num' is destroyed (no longer in scope)
// so this is now pointing at something that doesn't exist.
cout << (*pNums[0]) << endl;
I'm learning c++ and I'm trying to ask the user to input 4 numbers in a function, and then simply print the array.
int getFourNums();
int main(int argc, char** argv){
int getNums;
getNums = getFourNums();
cout << "The array is: " getNums << endl;
}
int getFourNums(){
int i;
int myArray[4];
cout << "Enter 4 nums: ";
for(i = 0; i < 4; i++){
cin >> myArray[i];
}
return myArray[i];
As of now, it's letting me get the four numbers, but the result that's printing is "The array is: 0." I'm not quite sure why the array is seemingly not populating.
Your fundamental problem is that int getFourNums() can only return a single integer, not an array of them. The next problem is that functions cannot return raw arrays for historical reasons. Your choices are to return a std::array, a struct containing the array, pass the array by reference into the function, or return a std::vector. My preference for this application is a std::vector - it is flexible, and although not quite as efficient as std::array, you should probably default to std::vector unless you have a good reason otherwise. Your getNums code would then look like:
std::vector<int> getFourNums() {
std::vector<int> result;
cout << "Enter 4 nums: ";
for(int i = 0; i < 4; i++){
int v;
cin >> v;
result.push_back(v);
}
return result;
}
To print the vector, see this question. My personal preference would be a range-based for loop over the vector; your tastes may vary.
One issue in your code is that a loop like
for(i = 0; i < 4; i++){
cin >> myArray[i];
}
will end up with i==4. Hence, return myArray[i] will exceed array bounds and/or access an uninitialised value then and yield undefined behaviour.
The main issue, however, is that in C++ you'll follow a very different approach and use collection types like std::vector instead of plain arrays. See the following code illustrating this. Hope it helps.
#include <vector>
#include <iostream>
std::vector<int> getFourNums(){
int val;
std::vector<int> result;
cout << "Enter 4 nums: ";
for(int i = 0; i < 4; i++){
cin >> val;
result.push_back(val);
}
return result;
}
int main(int argc, char** argv){
std::vector<int> fourNums = getFourNums();
for (auto i : fourNums) {
cout << i << endl;
}
}
int getFourNums() will only let you return one int, not the whole array and return myArray[i]; is out of bounds since i == 4. You can only use the range [0,3] as indices for your array. Here's a reworked version with comments in the code.
#include <iostream>
#include <vector>
// don't do "using namespace std;" since it includes
// a lot of stuff you don't need.
// Here's a function that will return a vector of int's
// It'll behave much like a C style array
// but can have variable length and you can use
// a lot of standard functions on it.
std::vector<int> getNums(size_t count) {
// The "array" we'll return with "count" number of
// default constructed int:s (they will all be 0):
std::vector<int> myArray(count);
std::cout << "Enter " << count << " nums: ";
// A range based for loop that will go through
// all int:s in "myArray". "num" will be
// a reference to each int in the vector which
// means that if you change the value of "num",
// you'll actually change the value in the vector.
for(int& num : myArray) {
// read values into the int currently
// referenced by num
std::cin >> num;
}
// return the vector by value
return myArray;
}
// Put main() last so you don't have to forward declare the functions
// it uses
int main() {
// call getNums with the value 4 to read 4 int:s
std::vector<int> Nums = getNums(4);
std::cout << "The array is:";
// print each int in the vector. There's no need to use
// a reference to the int:s here since we won't be changing
// the value in the vector and copying an int is cheap.
for(int num : Nums) {
std::cout << " " << num;
}
// std::endl is rarely good when you only want to output a newline.
// It'll flush the buffer with is costly.
// Make a habit of using "\n" in most cases.
std::cout << "\n";
}
I see that you want to return entire array but just look at your return type:
int getFourNums()
You're returning an integer right? In this situation the returned integer is always myArray[4]. Be aware that it's an integer value, you're returning something that doesn't belong to you actually!
So what to do? I suggest you to pass your array to function like this:
void getFourNums(int myArray[]){
int i;
cout << "Enter 4 nums: ";
for(i = 0; i < SIZE; i++){
cin >> myArray[i];
}
}
Now you filled your array. How to print your array then? We can't simply give our array name and tell cout to print it like you did (you couldn't actually!). Nothing magical here. We're going to print your array's element one by one:
void printFourNumbers(int array[])
{
for(int i = 0 ; i < SIZE ; ++i)
{
cout << array[i] << endl;
}
}
Finally whole code looks like this:
#include <iostream>
using namespace std;
const int SIZE = 4;
void getFourNums(int myArray[]);
void printFourNumbers(int array[]);
int main(int argc, char** argv){
int myArray[SIZE];
getFourNums(myArray);
printFourNumbers(myArray);
}
void getFourNums(int myArray[]){
int i;
cout << "Enter 4 nums: ";
for(i = 0; i < SIZE; i++){
cin >> myArray[i];
}
}
void printFourNumbers(int array[])
{
for(int i = 0 ; i < SIZE ; ++i)
{
cout << array[i] << endl;
}
}
So I recently started learning C++ and I'm trying to figure out how to access a multidimensional vector or a vector with vectors stored inside of it. I've looked all over and I can't find exactly what I'm looking for. I want to be able to test the contents of each vector inside of the multidimensional vector by printing them out. Also whenever I try to see the size of the vector after each iteration, I'm getting random constants for each iteration. They look like they may be memory locations but I'm not sure.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numofArrays;
int numofQueries;
vector<vector<int>> arr(numofArrays);
cin >> numofArrays;
cin >> numofQueries;
int arrSize;
int number;
vector<int> indArr;
// Outer loop, appends vectors containing ints to a multidimensional vector
for (int i = 0; i < numofArrays; i++) {
cin >> arrSize; // Getting number of elements from user
// Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
for (int x = 0; x < arrSize; x++) {
cin >> number;
indArr.push_back(number);
cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added.
}
arr.push_back(indArr);
cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
indArr.clear(); // Empties vector for next iteration
}
return 0;
}
As I am very new to C++, I welcome constructive criticism.
Newly Revised Code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numofArrays;
int numofQueries;
cin >> numofArrays;
cin >> numofQueries;
vector<vector<int>> arr(numofArrays);
// Outer loop, appends vectors containing ints to a multidimensional vector
for (int i = 0; i < numofArrays; i++) {
int arrSize;
vector<int>indArr;
cin >> arrSize; // Getting number of elements from user
indArr.resize(arrSize); // Resizing array for next values
// Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
for (int x = 0; x < arrSize; x++) {
int number;
cin >> number;
indArr.push_back(number);
cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added.
}
arr.push_back(indArr);
cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
}
int test = arr[0][0];
cout << test;
return 0;
}
Just some tips:
vector<vector<int>> arr(numofArrays); constructs a vector of vectors of size numofArrays. But numofArrays is not yet read from cin, so it is an undefined number. Care with the order of operations.
arr.push_back(indArr); adds a vector at the end of arr.
Hence, in your loop, even if you fix the order of reading numofArrays and declaring arr, you start with an array of size numofArrays, and each cycle you add an element. You will end up with arr holding 2 times numofArrays elements.
Also, indArr is a temporary variable, which you reset each cycle. Although there is nothing wrong about what you are doing, in general is better to keep each variable limited to its scope. If you define indArr inside the loop, it will be automatically cleared and re-created. May not be the most efficient way to do things in all cases, but I'd leave this details for later.
Something like this should work for you:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int numofArrays;
int numofQueries;
cin >> numofArrays;
cin >> numofQueries; // this is an unused variable
vector<vector<int> > arr;
// Outer loop, set vectors containing ints to a multidimensional vector
for (int i = 0; i < numofArrays; i++) {
int arrSize;
vector<int> indArr;
cin >> arrSize; // Getting number of elements from user
// Inner loop, gets user inputted values then creates a vector which is added to the multidimensional vector
for (int x = 0; x < arrSize; x++) {
int number;
cin >> number;
indArr.push_back(number);
cout << "Last number added to vector: " << number << endl; // Checking to see if correct numbers are being added.
}
arr.push_back(indArr);
cout << "Multidimensional Vector size: " << arr.size() << endl; // Checking size of vector after each iteration
}
return 0;
}
The way you're inspecting the size is correct. For example in your code you're printing the integer number of elements in the first dimension of the array arr.
The odd results you're getting is because of how you're initializing the vector. In the constructor for arr there you specify a size describing the number of default-constructed elements to fill the vector with. However you're providing numofArrays which is an undefined value. Hence you receive random looking vector sizes when you print this out.
The real size of the vector arr's first dimension will be whatever this undefined value is plus one, due to the push_back operation.
I am trying to create an array using cin to define the size. While that seems to be working (based on what I currently have), none of the other stuff I want to do seems to be working.
For instance, I want to use a for loop to find the smallest int in the array since I will then need to compare it with all the other ints in the array, but no matter where I have the statement to return the smallest int, it does not do it.
What am I doing wrong?
#include <iostream>
using namespace std;
int main(){
int userSize;
cout << "Please define size of array: ";
cin >> userSize;
int *duckArray = new int[userSize];
for (int i = 0; i < userSize; i++) {
cout << "Please enter a number into the array: ";
cin >> duckArray[i];
}
int smallest = duckArray[0];
for (int i = 0; i < userSize; i++){
if (duckArray[i] < smallest){
smallest = duckArray[i];
cout << smallest << endl;
}
}
//cout << smallest << endl;
return 0;
}
Your code is working if you change this:
for (int i = 0; i < userSize; i++){
if (duckArray[i] < smallest){
smallest = duckArray[i];
}
}
cout << smallest << endl;
This will find and print the smallest number entered.
Arrays in C++ must have their size declared to the compiler at runtime. Other people are going to explain how you can buffer memory to simulate a dynamically allocating arrays. You can also have your Array at a given size and as the user adds and removes, you can reject inputs over the current size.
I highly recommend you look into Vectors. Vectors are much like ArrayLists in Java. They are a form of higher level collections that resize themselves as you add more elements to them.
For a program I must use an array and not vector. I have to take in user's input, and it's a indefinite amount of them. The user can type in 5 values, or 50. I am absolutely stumped as to how to go about doing this. Using a for loop for example:
Int a[10];
Int b;
For (int i=0; i<10; i++)
{
Cout<<"enter values:";
Cin>>b;
A[i]=b;
}
With this I can take an array of 10 of user defined variables but how would I go about making it a dynamic size? Thank you for the help!
The size of a static array must be known at compile time, otherwise you must use a dynamic array. For example
#include <iostream>
int main()
{
// Determine how many total entries are expected
int entries;
std::cout << "How many values do you want to enter?" << std::endl;
std::cin >> entries;
// Allocate a dynamic array of the requested size
int* a = new int[entries];
// Populate the array
for (int i = 0; i < entries; ++i)
{
std::cout << "enter a value: ";
std::cin >> a[i];
std::cout << std::endl;
}
// Clean up your allocated memory
delete[] a;
return 0;
}