Creating a 2D array with user input [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am trying to make a 2D array where user inputs the number of elements that array can take, also the elements inside the array. I think I manage to create the array, but when I try to put some elements inside it, for example 2x2 array and putting 2 as all of its elements i get this as the output. Here is the code:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
int rowCount,colCount;
cout<<"Enter the number of rows in Grid-Land:";
cin>>rowCount;
cout<<"Enter the number of columns in Grid-Land:";
cin>>colCount;
int** arr = new int*[rowCount];
for(int i = 0; i < rowCount; ++i)
arr[i] = new int[colCount];
cout<<"Enter the garbage amounts at the nodes of the MxN Grid-Land:"<<endl; //Elements of the array
for(int i=0; i<rowCount; i++){
for (int j=0; i<colCount; i++)
cin>>arr[i][j];
}
cout<<"\nThe 2-D Array is:\n";
for(int i=0;i<rowCount;i++){
for(int j=0;j<colCount;j++){
cout<<"\t"<<arr[i][j];
}
cout<<endl;
}
return 0;
}

It's a typo. Instead of using the "j" variable in the inner loop while taking the input, you have used the "i" variable.

You got a typo in the for loop in which you prompt the user for the values of the array. You switched j's for i's so you're really only iterating over one column and never prompting the user for the rest of the values.
Change this
for(int i=0; i<rowCount; i++){
for (int j=0; i<colCount; i++)
cin>>arr[i][j];
}
}
For this
for(int i=0; i<rowCount; i++){
for (int j=0; j<colCount; j++)
cin>>arr[i][j];
}
}

Related

Why i can't assign value to the 2d vector of char? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 months ago.
Improve this question
I am working on a code that needs vectors of vectors (2d vector) and I am trying to initialize this with '.' with the below code.
vector< vector< char >> vec;
for(int i=0; i < N ; i++)
{
vector<char> temp('#', N);
vec.push_back(temp);
}
I also tried
vector< vector < char >> vec;
for(int i=0; i<N ; i++)
{
vector< char > temp();
temp.assign('.' , N);
vec.push_back(temp);
}
My code is assigning value to the vector, but assigned value isn't '.' but something else. When I tried to print it, it is printing some weird gibberish output.
I also tried the simple way to assign value to my vector and it worked well. Below is the code.
vector< vector < char >> vec;
for(int i=0; i<N ; i++)
{
vector< char > temp;
for(int j=0;j<N;j++)
{
temp.push_back('#');
}
vec.push_back(temp);
}
Why does the first and second code isn't working similar to third. Or are they meant to be used with integers only and not char.
The problem is that you have supplied the arguments in the wrong order when creating the temp vector.
Replace vector<char> temp('#', N); with
vector<char> temp(N,'#');
Do the same with std::vector::assign.

Is this the correct implementation of Insertion Sort? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
OUTPUT
#include <iostream>
using namespace std;
int main()
{
//declaring the size of array and taking input from the user
int n = 0;
cout<<"Enter the Number of elements you want in the Array : ";
cin>>n;
//checking the user input
if(n <= 0)
{
cout<<"Not Possible\n";
return 1;
}
//declaring array of size 'n' and taking input from user
int list[n];
cout<<"Enter the Elements of the array of size "<<n<<" : ";
for(int i = 0; i < n; i++)
cin>>list[i];
//Insertion Sort
int swap = 0; //number of swaps
int comp = 0; //number of comparison
int temp; //temporary variable
for(int i = 0; i < n-1; i++)
{
for(int j = i+1; j > 0; j--)
{
if(list[j] < list[j-1])
{
//swapping equivalent to shifting
temp = list[j-1];
list[j-1] = list[j];
list[j] = temp;
comp++;
swap++;
}
else
{
comp++;
break;
}
}
//printing the iteration
cout<<"Iteration "<<(i+1)<<" : ";
for(int k = 0; k < n; k++)
cout<<list[k]<<" ";
cout<<"\n";
}
cout<<"\nSwap : "<<swap<<"\n";
cout<<"Comparison : "<<comp<<"\n";
cout<<"Sorted Array : ";
for (int i = 0; i < n; i++)
{
cout<<list[i]<<" ";
}
return 0;
}
Is this implementation of insertion sort correct because I have seen many implementation online using while loop and other things?
If not can you point out what is wrong?
Thanks in advance
link - https://github.com/ish-u/DiscreteStructures/blob/master/InsertionSort.cpp
No, this is a different type of sort, known as bubble sort. It still sorts, but insertion sort works in a different way, by keeping the array sorted at all times (moving elements if a new insertion would break the ordering).
So instead of just tagging new elements to the end of the array where you read them from cin, you should place each element directly in the right spot in the array. This will likely involve moving existing elements in order to keep the array sorted.
Note that your line
int list [n];
is wrong; you cannot allocate memory this way (and I'm surprised it even compiles). A better choice would be to use std::vector.

invalid types ‘int[int]’ for array subscript in 2D array [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Working on a project that reads integers from a file and puts them into a 2D array. I tested it with a 1D array but when I tried it with a 2D array I kept getting this error "invalid types ‘int[int]’ for array subscript" in my class called "Image" in this function:
void Image::read(int* arr)
{
//Nested loop that reads the file
for(int i = 0; i < height; i++)
{
for(int k = 0; k < width; k++)
{
inputFile >> arr[i][k]; //Here's where I get the error
}
}
}
And here is my main function:
int main()
{
Image test("colorado1.dat"); //File with the integers
test.setWidth(500);
test.setHeight(500);
int array[test.getHeight()][test.getWidth()];
test.read(array);
//Loop to test if the function worked
for(int i = 0; i < 500; i++)
{
for(int k = 0; k < 500; k++)
{
cout << array[i][k] << " ";
}
}
}
Take a look at this function signature:
void Image::read(int* arr);
Here, you've said that the type of arr is int*, a pointer to an integer. As a result, when you write
arr[i][k]
C++ says "hold on a second... arr is an int *, so arr[i] is an int, and you can't apply the square bracket operator to a pair of int values."
To fix this, you'll need to change up the way that you handle parameters to this function. You could either pass in an int**, which would represent a pointer to a pointer to an int, or consider using something like std::vector<std::vector<int>>, which is safer and easier to use.

C++ Arrays, For Loop and String Data Types [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm trying to take in 4 strings into an array using a for loop. After that for loop finishes, I'm trying to display the 4 strings using a for loop. I don't know where my error is. Any help and guidance would be appreciated.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
//declare variables
string array[4] ;
cout<<"input 4 strings"<<endl;
for (int i=1; i<4; i++)
{
getline(cin,array[i]);
}
cout << "here are your 4 strings" << endl;
for (int j=0; j<4; j++)
{
cout<<array[j]<<endl;
}
system("pause");
return 0;
} // end of main function
Your first loop should start at i = 0, not i = 1.
You are taking in three strings,
for (int i=1; i<4; i++)
{
getline(cin,array[i]);
}
and printing four...
for (int j=0; j<4; j++)
{
cout<<array[j]<<endl;
}

Pointer Array Over Then 650k Element [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an integer array list and I want to make left right pointer array list to access this. But for more than 690.000 elements (for example 700.000) the program stops and says `Segmentation fault (core dumped). I have used std::vector before but I have to use simple array list because of speed. And This code is not about my program it is very simple way to tell my problem with code. And I have to use global variable because this code run in a class methods.
int k=698900;
int n1=k/2;
int n2=k/2;
int *left[n1];
int *right[n2];
int list[k];
for(int i=0; i<k; i++){
list[i] = i;
}
for(int i=0; i<n1; i++){
left[i] = &list[i];
}
for(int i=0; i<n2; i++){
right[i] = &list[i];
}
for(int i=0; i<n2; i++){
cout << *right[i] << endl;
}
As #PaulR already said in the comments your problem is a stack overflow. You can solve this problems a few ways. If you're categorically against changing your code then you can adjust the stack size in your compiler. How exactly that is done depends on your compiler (gcc example and VS example). You can also adjust it at run-time but that's an OS dependent function, so I'm not going to go into details about that.
The solution that I would prefer is allocate the memory on the heap. This can be done by either allocating the arrays on the heap int **left = new int*[n1]; but then you have to remember to call delete []left; when you don't need the memory anymore. The far more elegant solution would be to use a std::vector instead.
I challenge you to compile this with all optimizations on and tell me that it's significantly slower than your code.
int k=698900;
int n1=k/2;
int n2=k/2;
//not that I condone storing raw pointers in a vector
//admittedly this is a rather dangerous thing to do
//storing indices would be smarter because upon adding
//elements the memory could be relocated
std::vector<int *> left(n1);
std::vector<int *> right(n2);
std::vector<int> list(k);
for(int i=0; i<k; i++){
list[i] = i;
}
for(int i=0; i<n1; i++){
left[i] = &list[i];
}
for(int i=0; i<n2; i++){
right[i] = &list[i];
}
for(int i=0; i<n2; i++){
cout << *right[i] << endl;
}
Like #PaulR mentioned you can also allocate the arrays statically (static int *left[n1];) or globally (outside of any function or class declaration/definition)