So I have this C++ program that contains .h file and main.cpp. In .h I have this class:
class Plaza
{
public:
int length;
double x;
double y;
Plaza();
~Plaza();
};
In main.cpp, I am trying to enter the data using for loop, and I manage to store data for int i = 0 state, but when i is increased, no data that has been entered is being stored into array. For the inside loop, I tried to put j < n, j < n-1 and j < n+1, but it's not working. How can I store all the data and print it out?
#include <iostream>
#include "Plaza.h"
using namespace std;
int main() {
int n;
Plaza *obj1;
cout << "Enter limit number (N): ";
cin >> n;
obj1 = new Plaza[n];
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
for (int i = 0; i < n; i++) {
cout << i + 1 << ". " << obj1[i].x << " " << obj1[i].y << " Length=" << obj1[i].length;
}
delete[] obj1;
system("pause");
return 0;
}
This is the print I get:
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
Here's your culprit. Get rid of the inner for-loop (not the cin-statements, just the for... line and its closing bracket) and replace obj[j] with obj[i]. You are currently repeatedly writing to obj[0].
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
for (int j = 0; j < 1; j++) {
cin >> obj1[j].length;
cin >> obj1[j].x >> obj1[j].y;
}
}
Why there is a need of second for loop., if you check the j value, it is always 0 so only one value is inserted.,
try this
for (int i = 0; i < n; i++) {
cout << "Enter length, x and y for " << i + 1 << ". plaza: " << endl;
cin >> obj1[i].length;
cin >> obj1[i].x >> obj1[i].y;
}
}
Related
This question already has answers here:
What is error in code for adding 3*3 matrix? [duplicate]
(2 answers)
Closed 1 year ago.
I'm trying to input a 2D array in c++ but my code doesn't even get to calculation lines:
int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];
for (int i = 1; i <= dim1; i++) {
for (int j = 1; j <= dim2; j++) {
cin >> arr[i][j];
}
}
for (int i = 1; i <= dim1; i++) {
for (int j = 1; j <= dim2; j++) {
cout << arr[i][j] << ' ';
}
cout << endl;
}
It's simple, you should start inputing an array in c++ from 0, so your loop should begin with i = 0 and should be stopped when i == n.
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
Your code has two problems.
First, VLA arrays is not valid C++ code. You have to use std::vector.
Second. C++ array indexes start at 0 and end at size - 1. But you are looping from 1 to n. This creates out-of-bounds.
Your code should look like this:
int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr;
arr.resize(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cin >> arr[i][j];
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << endl;
}
First in C++, the size of an array must be a compile-time constant.So, take for example the following code snippets:
int n = 10;
int arr[n]; //INCORRECT because n is not a constant expression
The correct way to write the above would be:
const int n = 10;
int arr[n]; //CORRECT
Similarly, the following(which you did in your code example) is incorrect:
int dim1, dim2;
cout << "Enter first dimension: ";
cin >> dim1;
cout << endl << "Enter second dimension: ";
cin >> dim2;
int arr[dim1][dim2];//INCORRECT
You should instead use std::vector<> as shown below:
#include <iostream>
#include <vector>
int main()
{
int dim1, dim2;
std::cout << "Enter first dimension: ";
std::cin >> dim1;
std::cout << std::endl << "Enter second dimension: ";
std::cin >> dim2;
std::vector<std::vector<int>> arr(dim1, std::vector<int>(dim2));
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cin >> arr[i][j];
}
}
for (int i = 0; i < dim1; i++) {
for (int j = 0; j < dim2; j++) {
std::cout << arr[i][j] << ' ';
}
std::cout << std::endl;
}
return 0;
}
I want to find the factors of a product by using arrays to check whether they equal it or not
the values do not print
here is the code
#include <iostream>
using namespace std;
int main() {
int arr[5] = { 1,3,5,7,2 };
int arr1[5] = { 0,6,5,4,9 };
int X;
cout << "Please enter X:"; cin >> X;
for (int i = 0, j = 0; i < 5 && j < 5; ++i, ++j) {
if (arr[i]*arr[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
Use this nested loops
for (int i = 0; i < 5 ;++i) {
for(int j=0 ;j<5;++j){
if (arr[i]*arr1[j]==X) {
cout << arr[i] << " ";
cout << arr1[j] << " ";
}
}
}
I am stuck on what to do next... The program is suppose to check to see if entered Zero-One Matrix is an Equivalence relation (transitive, symmetric, and reflexive) or not. I am still new to C++ (started this semester). I know how to create the matrix using vector but not on how to check if it is equivalence relation or not..
I assume I need to use boolean function but I'm stuck on what I need to put in as an argument or if this is correct. My original thought was... so for symmetric it will look like (which I know this goes after #include and beofre int main(). Any help would be awesome.
bool isSymmetric(vector<int> &vect, int Value)
{
for (int i = 0; i < Value; i++)
for (int j = 0; j < Value; j++)
if (vect[i][j] != vect[j][i])
return false;
return true;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector< vector<int> > vec;
cout << "NxN matrix N: ";
int Value;
cin >> Value;
cout << Value << "x" << Value << " matrix\n";
for (int i = 0; i < Value; i++) {
vector<int> row;
for (int j = 0; j < Value; j++) {
cout << "Enter a number (0 or 1): ";
int User_num;
cin >> User_num;
while (User_num != 0 && User_num != 1) {
cout << "Invalid Entry! Enter 0 or 1!\n";
cout << "Enter a number (0 or 1): ";
cin >> User_num;
}
row.push_back(User_num);
}
vec.push_back(row);
}
cout << endl;
for (int i = 0; i < Value; i++) {
for (int j = 0; j < Value; j++) {
cout << vec[i][j] << " ";
}
cout << endl;
}
cout << endl;
system("pause");
return 0;
}
Here is my full code:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdio.h>
#include <string.h>
using namespace std;
int main(){
int ***my3DArray;
int length, width, height;
bool doAgain = true;
string answer("");
string yes("y");
do{
cout << "Enter length: ";
cin >> length;
cout << "\nEnter width: ";
cin >> width;
cout << "\nEnter height: ";
cin >> height;
srand((unsigned)time(NULL));
my3DArray = new int**[length];
for (int i= 0; i < length; ++i){
my3DArray[i] = new int*[width];
for(int j = 0; j< width; ++j){
my3DArray[i][j] = new int[height];
for(int k = 0; k < height; ++k){
my3DArray[i][j][k] = rand()%100;
cout << my3DArray[i][j][k] << " ";
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
cout << endl;
}
cout << endl;
}
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
delete[]my3DArray[i][j];
}
delete[]my3DArray[i];
}
delete[]my3DArray;
my3DArray = NULL;
cout << "Again? (y, n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
}
This prints a 3D array of integers, I need to write a function called tick that returns the element of user specified coordinates i j k. That part of the code is this
do{
for (int i = 0; i < length; ++i){
for (int j = 0; j < width; ++j){
for (int k=0; k< height; k++){
cout << "\n\nEnter coodinates: ";
cin >> i;
cin >> j;
cin >> k;
cout << "Element is " << my3DArray[i][j][k];
}
cout << endl;
}
cout << endl;
}
cout << "Find another element? (Y/n)";
cin >> answer;
if(answer.compare(yes) == 0)
doAgain = true;
else doAgain = false;
}
while(doAgain == true);
when I have this in my code, it doesnt print out the 3D array and I get a segmentation fault when it's supposed to print the element. Any Ideas? Thanks in advance
instead of int ***my3DArray use int my3DArray[256][256] when declaring an array is just a pointer to the first item in the list. you then run a for loop to go through that list.
Create a function that prints the array to the screen as a table with the appropriate rows and columns. Use setw() to ensure the numbers have enough room. You may assume the numbers are no more than 3 digits each. Include the row and column labels.
In main, ask the user to supply a row and column and a value (no more than 3 digits), then put the value into the array at that row and column. Print the result and ask the user for another row, column and value. Repeat until the user is finished.
Once finished, compute and print the total of the values in the array.
#include <iostream>
#include <iomanip>
using namespace std;
const int ROWS= 4;
const int COLS = 3;
const int WIDTH = 4;
const char YES = 'y';
int total = 0;
void print(int arr[ROWS][COLS]);
int main()
{
int arr[ROWS][COLS];
char ans = YES;
int val;
for (int r = 0; r < ROWS; r++){
for (int c = 0; c < COLS; c++)
arr[r][c] = 0;}
while (tolower(ans) == YES){
int row = -1;
int col = -1;
cout << "Row? ";
cin >> row;
cout << "Columns? ";
cin >> col;
while (row < 0 || row >=ROWS){
cout << "Only value under " << ROWS << " is accepted, try again: ";
cin >> row;
}
while (col < 0 || col >= COLS){
cout << "Only value under " << COLS << "is accepted, try again: ";
cin >> col;
}
cout << "Value? ";
cin >> val;
arr[row][col] = val;
print(arr);
cout << "Again (y/n)? ";
cin >> ans;
}
for (int r = 0; r < ROWS; r++){
for (int c = 0; c < COLS; c++){
total += arr[r][c];
}
}
cout << "Sum of all values is " << total << endl;
// Print array with labels
// get input from user
// print array again - repeat until user wishes to quit
return 0;
}
void print (const int arr[ROWS][COLS])
{
cout << endl << endl;
for (int i = 0 ; i < COLS; i++)
cout << setw(WIDTH) << i;
cout << endl;
for (int r = 0; r < ROWS; r++)
cout << setw(WIDTH) << r << "|";
for (int r = 0; r < ROWS; r++)
for (int c = 0; c< COLS; c++)
cout << setw(WIDTH) << arr[r][c];
cout << endl << endl;
}
I dont know where I did wrong on this one but when I complie, I got the LD return 1 exit status error, Can you please help?
The definition and the declaration of your print function are different.
Change the declaration of your print() function.
void print(int arr[ROWS][COLS]);
to
void print(const int arr[ROWS][COLS]);