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.
Related
learning graph theory in c++ here.
Sorry for the C-style codes.
I got an segmentation fault of my codes. I understand the meaning of it but have not learnt how to debug with IDE yet.
However I feel the bug is somewhere in my spanningtree() function. Could anyone point me out what could went wrong? The program is meant to print out the cost matrix, the minimum distance path and the total path cost. However, it exited after inputting.
#include <iostream>
using namespace std;
class prims
{
private:
int no_of_edges, no_of_nodes;
int graph[10][10],visited[10],mindist[10];
public:
void input();
void output();
void spanningtree();
prims()
{
no_of_edges = no_of_nodes = 0;
for (int i = 0; i<10; i++)
{
//assign visited minimum distance array to 0
visited[i] = mindist[i] = 0;
for (int j = 0; j<10; j++)
{
graph[i][j] = 0;
}
}
}
};
void prims::input()
{
int vertex1, vertex2, cost;
cout << "Enter no_of_nodes ";
cin >> no_of_nodes;
cout << "Enter the no_of_edges ";
cin >> no_of_edges;
for (int i = 0; i< no_of_edges; i++)
{
cout << "Enter vertex1 ";
cin >> vertex1;
cout << "Enter vertex2 ";
cin >> vertex2;
cout << "Enter the cost of " << vertex1 << " and " << vertex2 << " ";
cin >> cost;
graph[vertex1][vertex2]=graph[vertex2][vertex1]=cost;
}
}
void prims::output()
{
for (int i = 0; i < no_of_nodes; i++)
{
cout << endl;
for (int j = 0; j< no_of_nodes; j++)
{
cout.width(4);
cout << graph[i][j];
}
}
}
void prims::spanningtree()
{
int min = 9999, row, col, index = 0;
for (int i = 0; i < no_of_nodes; i++)
{
for(int j = i; j < no_of_nodes; j++)
{
if(graph[i][j]<min&&graph[i][j]!=0)
{
min = graph[i][j];
row = i;
col = j;
}
}
}
visited[row]=visited[col]=1;
mindist[index++]=min;
for (int i = 0; i < no_of_nodes - 2; i++)
{
min = 9999;
for (int j = 0; j < no_of_nodes; j++)
{
if(visited[j]==1)
{
for(int k = 0; j < no_of_nodes; k++)
{
if(graph[j][k]<min&&graph[j][k]!=0 && visited[k]==0)
{
min = graph[j][k];
row = j;
col = k;
}
}
}
}
mindist[index++]=min;
visited[row]=visited[col]=1;
}
int total = 0;
cout << endl;
cout << "Minimum distance path is ";
for (int i=0; i < no_of_nodes-1; i++)
{
cout << " " << mindist[i] << " ";
total = total + mindist[i];
}
cout << endl << "Total path cost is " << total;
}
int main()
{
prims obj;
obj.input();
obj.spanningtree();
obj.output();
return 0;
}
Taking some credits from the helpful comments/answers. Here is my revised codes. The main issue was the typo in one of the loop for(int k = 0; j < no_of_nodes; k++).
using namespace std;
class prims
{
private:
int no_of_edges, no_of_nodes;
int graph[10][10],visited[10],mindist[10];
public:
void input();
void output();
void spanningtree();
prims()
{
no_of_edges = no_of_nodes = 0;
for (int i = 0; i<10; i++)
{
//assign visited minimum distance array to 0
visited[i] = mindist[i] = 0;
for (int j = 0; j<10; j++)
{
graph[i][j] = 0;
}
}
}
};
void prims::input()
{
int vertex1, vertex2, cost;
cout << "Enter no_of_nodes ";
cin >> no_of_nodes;
cout << "Enter the no_of_edges ";
cin >> no_of_edges;
for (int i = 0; i< no_of_edges; i++)
{
cout << "Enter vertex1 ";
cin >> vertex1;
cout << "Enter vertex2 ";
cin >> vertex2;
cout << "Enter the cost of " << vertex1 << " and " << vertex2 << " ";
cin >> cost;
graph[vertex1][vertex2]=graph[vertex2][vertex1]=cost;
}
}
void prims::output()
{
for (int i = 0; i < no_of_nodes; i++)
{
cout << endl;
for (int j = 0; j< no_of_nodes; j++)
{
cout.width(4);
cout << graph[i][j]<<" ";
}
}
}
void prims::spanningtree()
{
int min = 9999, row, col, index = 0;
for (int i = 0; i < no_of_nodes; i++)
{
for(int j = i; j < no_of_nodes; j++)
{
if(graph[i][j]<min&&graph[i][j]!=0)
{
min = graph[i][j];
row = i;
col = j;
}
}
}
visited[row]=visited[col]=1;
mindist[index++]=min;
for (int i = 0; i < no_of_nodes - 2; i++)
{
min = 9999;
for (int j = 0; j < no_of_nodes; j++)
{
if(visited[j]==1)
{
for(int k = 0; k < no_of_nodes; k++)
{
if(graph[j][k]<min&&graph[j][k]!=0 && visited[k]==0)
{
min = graph[j][k];
row = j;
col = k;
}
}
}
}
mindist[index++]=min;
visited[row]=visited[col]=1;
}
int total = 0;
cout << endl;
cout << "Minimum distance path is ";
for (int i=0; i < no_of_nodes-1; i++)
{
cout << " " << mindist[i] << " ";
total = total + mindist[i];
}
cout << endl << "Total path cost is " << total << endl;
}
int main()
{
prims obj;
obj.input();
obj.spanningtree();
obj.output();
// return 0;
}
Your primary problem is not checking that indexes are in range (there is where c++ might help, but you can do it in c as well). Primary debugging tool - print. If you would print j and k before using them as array indexes you would solve your problem yourself
I would like to create this pyramid with C++, but I am a bit stuck. Can I get some help to complete my example?
6*****
*6****
**6***
***6**
****6*
*****6
int num1;
cout<<"please enter a size between 1-9: "<<flush;
cin>>num1;
for(int i = 0; i < num1; i++)
{
cout <<num1;
for(int j = 0; j <= i; j++)
{
cout<<"*";
}
cout << "\n";
}
6*
6**
6***
6****
6*****
6******
There are two ways you can handle this:
use 2 separate loops, one for the stars in front of the number, and another loop for the stars behind the number, eg:
#include <iostream>
using namespace std;
int main()
{
int num1;
cout << "please enter a size between 1-9: ";
cin >> num1;
for(int i = 0; i < num1; ++i)
{
for(int j = 0; j < i; ++j)
cout << "*";
cout << num1;
for(int j = num1-1; j > i; --j)
cout << "*";
cout << "\n";
}
return 0;
}
Live Demo
use a single loop that conditionally decides whether to output a star or the number depending on which position is currently being output:
#include <iostream>
using namespace std;
int main()
{
int num1;
cout << "please enter a size between 1-9: ";
cin >> num1;
for(int i = 0; i < num1; ++i)
{
for(int j = 0; j < num1; ++j)
{
if (j == i)
cout << num1;
else
cout << "*";
}
cout << "\n";
}
return 0;
}
Live Demo
You need asterisks before the number and after the number. So, let's expand your idea:
int num1;
cout<<"please enter a size between 1-9: "<<flush;
cin>>num1;
for(int i = 0; i < num1; i++)
{
//Asterisks before the number
for(int j = 0; j < i; j++)
{
cout<<"*";
}
cout <<num1;
//Asterisks after the number
for(int j = i; j < num1; j++)
{
cout<<"*";
}
cout << "\n";
}
There is a little improve that you could do: Refactorize in functions:
void writeRow(int length)
{
for (int i = 0; i < length; i++)
std::cout << "*";
}
// Some code...
for(int i = 0; i < num1; i++)
{
//Asterisks before the number
writeRow(i);
cout <<num1;
//Asterisks after the number
writeRow(num1-i);
cout << "\n";
}
You can create the entire string for each row by using the std::string constructor that takes a count and a character.
Basically here is the pattern:
Print 0 stars, the number 6, then num-1 stars.
Print 1 star, the number 6, then num-2 stars.
Print 2 stars, the number 6, then num-3 stars.
etc...
So the pattern is to build a string that consists of the stars before, the number 6, then the stars after, and for each row, you increment the stars before, and decrement the stars after.
So here is an example:
#include <string>
#include <iostream>
int main()
{
int num1 = 6;
int stars_before = 0;
int stars_after = num1 - 1;
for (int i = 0; i < num1; ++i)
std::cout << std::string(stars_before++, '*') << '6' << std::string(stars_after--, '*') << "\n";
}
Output:
6*****
*6****
**6***
***6**
****6*
*****6
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;
}
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;
}
}
The user enteres a number which is put in an array and then the array needs to be orinted backwadrds
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> x;
numbers[x];
}
for (int i = 5; i>0 ; i--)
{
cout << numbers[i];
}
return 0;
}
You're very close. Hope this helps.
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
int numbers[5];
/* Get size of array */
int size = sizeof(numbers)/sizeof(int);
int val;
for(int i = 0; i < size; i++) {
cout << "Enter a number: ";
cin >> val;
numbers[i] = val;
}
/* Start index at spot 4 and decrement until k hits 0 */
for(int k = size-1; k >= 0; k--) {
cout << numbers[k] << " ";
}
cout << endl;
return 0;
}
You are very close to your result but you did little mistakes, the following code is the correct solution of the code you have written.
int main()
{
int numbers[5];
int x;
for (int i = 0; i<5; i++)
{
cout << "Enter a number: ";
cin >> numbers[i];
}
for (int i = 4; i>=0; i--)
{
cout << numbers[i];
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//get size of the array
int arr[1000], n;
cin >> n;
//receive the elements of the array
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
//swap the elements of indexes
//the condition is just at "i*2" be cause if we exceed these value we will start to return the elements to its original places
for (int i = 0; i*2< n; i++)
{
//variable x as a holder for the value of the index
int x = arr[i];
//index arr[n-1-i]: "-1" as the first index start with 0,"-i" to adjust the suitable index which have the value to be swaped
arr[i] = arr[n - 1 - i];
arr[n - 1 - i] = x;
}
//loop for printing the new elements
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
return 0;
}
#include <iostream>
using namespace std;
int main() {
//print numbers in an array in reverse order
int myarray[1000];
cout << "enter size: " << endl;
int size;
cin >> size;
cout << "Enter numbers: " << endl;
for (int i = 0; i<size; i++)
{
cin >> myarray[i];
}
for (int i = size - 1; i >=0; i--)
{
cout << myarray[i];
}
return 0;
}
of course you can just delete the cout statements and modify to your liking
this one is more simple
#include<iostream>
using namespace std;
int main ()
{
int a[10], x, i;
cout << "enter the size of array" << endl;
cin >> x;
cout << "enter the element of array" << endl;
for (i = 0; i < x; i++)
{
cin >> a[i];
}
cout << "reverse of array" << endl;
for (i = x - 1; i >= 0; i--)
cout << a[i] << endl;
}
answer in c++. using only one array.
#include<iostream>
using namespace std ;
int main()
{
int array[1000] , count ;
cin >> count ;
for(int i = 0 ; i<count ; i++)
{
cin >> array[i] ;
}
for(int j = count-1 ; j>=0 ; j--)
{
cout << array[j] << endl;
}
return 0 ;
}
#include <iostream>
using namespace std;
int main ()
{
int array[10000];
int N;
cout<< " Enter total numbers ";
cin>>N;
cout << "Enter numbers:"<<endl;
for (int i = 0; i <N; ++i)
{
cin>>array[i];
}
for ( i = N-1; i>=0;i--)
{
cout<<array[i]<<endl;
}
return 0;
}