So me and my group have been working on this project for a few days we can get the top part too draw when we step into it but it does not run and we are not sure why it wont.
The first function is to draw the top and the second is to draw the bottom.
The top seems to draw fine i am not sure why the code will not even run.
#include<iostream>
using namespace std;
void drawTopPart(int userNum);
void drawBottomPart(int userNum);
int main() {
//declarations
int userNum;
//get user input
cout << "Enter an odd number from 1 to 15: ";
cin >> userNum;
//output
drawTopPart(userNum);
drawBottomPart(userNum);
cout << endl;
system("pause");
return 0;
}
void drawTopPart(int userNumPar) {
int z = 1;
int size;
cin >> size;
for (int i = 0; i <= size; i++) {
for (int j = size; j>i; j--) {
cout << " ";
}
cout << "*";
if (i>0) {
for (int k = 1; k <= z; k++) {
cout << " ";
} z += 2; cout << "*";
}
cout << endl;
}
}
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
cin >> size;
for (int i = 0; i <= size - 1; i++) {
for (int j = 0; j <= i; j++) {
cout << " ";
}
cout << "*";
for (int k = 1; k <= z; k++) {
cout << " ";
}
z -= 2;
if (i != size - 1) {
cout << "*";
}
}
You have two mainly errors.
First,
void drawBottomPart(int userNumPar){
int z -= 4;
int size;
int z -= 4; have syntax error.You can change it int z = 17;
Second,you forget endl here.
if (i != size - 1) {
cout << "*";
}
cout << endl;
Related
I am trying to get the height of these slashes to be a certain length based on input. So far, I have:
#include <iostream>
using namespace std;
int main() {
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
}
I need it to then reverse and go back.
It prints:
//
////
//////
If the user entered 3.
It should print:
//
////
//////
////
//
Can anyone lead me in the right direction? I am new to cpp.
You can use a different kind of loop and add a bool variable to track when the program have reached "n". Then, after the program reaches "n", it sets the bool variable to true and starts to substract until i equals 0
Code below, read comments and ask if you have any further questions:
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You have entered: " << n << "\n";
int i = 1;
bool reachedN = false; // tells if [i] has reached [n]
while (i != 0)
{
// Print required slashes
for (int j = 1; j <= i; j++)
{
cout << "//";
}
cout << '\n'; // new line
// Add until i == n, then substract
if (i == n)
{
reachedN = true;
}
if (reachedN)
{
--i;
}
else
{
++i;
}
}
}
If you enter 3, the output is the following:
This is one way to achieve that:
#include <iostream>
using namespace std;
int main() {
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
for (int i = n - 1; i > 0; i--) {
for (int j = 1; j <= i; j++)
cout << '/' << '/';
cout << "\n";
}
}
This is a shorter solution with only two for-loops.
#include <iostream>
using namespace std;
int main()
{
int n = 0;
cout << "Enter value: ";
cin >> n;
cout << "You entered: " << n << "\n";
n = n * 2 - 1;
int r = 0;
for (int j = 0; j < n; j++)
{
if (j > n / 2) r--;
else r++;
for (int i = 0; i < r; i++)
{
cout << '/' << '/';
}
cout << "\n";
}
return 0;
}
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
so i got this code.
int n;
int m=1;
int a=9;
cout<<"Enter N Number = ";
cin>>n;
n=n*2-1;
for(int y=1;y<=n;y++)
{
for(int x=1;x<=n;x++)
{
if(x==y+n/2 || x==y-n/2 || x==n-y+1-n/2 || x==n-y+1+n/2)
{
if(m<=9)
{
cout<<m++;
}
else if(m>9&&a>0)
{
a--;
cout<<a;
}
}
else
{
cout<<" ";
}
}
cout<<endl;
}
This is what i get :
Diamond Shape(Fail)
And what i expected is there's no number "0" on the bottom of the shape, so after it printed the number 1 its bounce back to number 2,3 and so on
pardon for my bad english
Do you want it to be like this?
Check this out!
I can code in Pascal, C++ and Java.
PS: I am not a native English speaker too.
First Answer: This is not the best answer, I will improve it tomorrow afternoon.
#include <iostream>
using namespace std;
int get() {
static int counter = 0;
static bool reverse = false;
!reverse ? counter++ : counter--;
if (counter==10 or counter==0) reverse = !reverse;
counter==10 ? counter = 8 : 0;
counter==0 ? counter = 2 : 0;
return counter;
}
int main() {
int number;
cout << "Enter N Number = ";
cin >> number;
//forward:
for (int i = 0; i < number; i++) {
for (int j = number-i-1; j > 0; j--)
cout << " ";
cout << get();
if (i!=0) {
for (int j = 0; j < i * 2 -1; j++) cout << " ";
cout << get();
}
cout << endl;
}
//backward:
for (int i = number-1; i > 0 ; i--) {
for (int j = number-i; j > 0; j--)
cout << " ";
cout << get();
if (i!=1) {
for (int j = i * 2 -3; j > 0; j--) cout << " ";
cout << get();
}
cout << endl;
}
return 0;
}
Second Answer: Improved loops
#include <iostream>
using namespace std;
int nextNumber() {
static int counter = 0;
static bool reverse = true;
!reverse ? counter++ : counter--;
if (counter==-1) counter=1;
if (counter==9 or counter==1) reverse = !reverse;
return counter;
}
int main() {
int number;
cout << "Enter N Number = ";
cin >> number;
int space = number;
int middle = -3;
for (int row = 0; row < number*2-1; row++) {
if(row<number) {
space--;
middle += 2;
}
else {
space++;
middle -= 2;
}
for (int i = 0; i < space; i++)
cout << " ";
cout << nextNumber();
if(row!=0 and row!=number*2-2) {
for (int i = 0; i < middle; i++)
cout << " ";
cout << nextNumber();
}
cout << endl;
}
return 0;
}
I want to compare elements for each line of a matrix, from smallest to biggest.
But I don't want to print the sorted array I want to print the original position.
0 11.80 79.34 78.23
11.80 0 65.23 45.19
79.34 65.23 0 90.27
78.23 45.19 90.27 0
In this Matrix for the first line I wanna print 1, 2, 4, 3
My Code so far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main() {
string dummy;
double myArray[4][4];
int i;
int j;
int y;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
infile >> myArray[i][j];
if (!infile) {
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int x = myArray[i][j];
for (i = 0; i < 4; i++) {
for (j = 0; j < 4; j++) {
if(myArray[i][j] >= x) {
x = j;
}
else {
x = j + 1;
}
}
cout << x << endl;
}
return 0;
}
You need to maintain another matrix which has indices of every line and then apply the same operations on it that you are applying on each line of the original matrix to sort it.
Here is the code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void swap(int &x, int &y)
{
int temp = x;
x = y;
y = temp;
}
void swap(double &x, double &y)
{
double temp = x;
x = y;
y = temp;
}
int main()
{
string dummy;
double myArray[4][4];
int i;
int j;
int y;
int k;
ifstream infile("dist.dat");
cout << "Open file " << "dist.dat" <<" for reading." << endl;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
infile >> myArray[i][j];
if (!infile)
{
cout << "***There was a problem trying to read element [" << i << "][" << j << "]" << endl;
return 0;
}
}
}
infile.close();
cout << "Here's the array from the file" << endl;
cout << fixed << setprecision(2);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << myArray[i][j];
}
cout << endl;
}
cout << endl;
int sortedIndices[4][4];
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
sortedIndices[i][j] = j+1;
}
}
int x;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
x = j;
for(k = j+1; k < 4; k++)
{
if(myArray[i][k] < myArray[i][x])
{
x = k;
}
}
swap(sortedIndices[i][j], sortedIndices[i][x]);
swap(myArray[i][j], myArray[i][x]);
}
}
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
cout << setw(10) << sortedIndices[i][j];
}
cout<<endl;
}
return 0;
}
I'm printing random numbers enclosed in ascii boxes in a 6x6 grid. Having issues with printing out the grid.
Instead of having 6 columns, all my boxes and numbers are being printed out in 1 column. Been troubleshooting but cant seem to find the issue. Below is the code. Appreciate your assistance.
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy [6][6];
srand((unsigned)time(0));
int lowest=1111, highest=9999;
int range=(highest-lowest)+1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest+int(range*rand()/(RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
for(int j = 0; j < 6; ++j)
{
cout << char(218);
for (int y=0; y< 4; y++)
{
cout << char(196);
}
cout << char(191) <<endl;
cout << char(179) << arrayxy[i][j] << char(179) << endl;
cout << char(192);
for (int z=0; z< 4; z++)
{
cout << char(196);
}
cout << char(217) <<endl;
}
cout << endl;
}
cout << endl;
}
You should endl after all the columns get printed out, NOT before.
This is how I fixed your code that gives, hopefully, what you wanted to have.
For the record, I specifically changed those ASCII to * and & to make the console result more readable. You can change them back to what you want those characters to be again.
void WriteFrontLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
void WriteEndLine(std::size_t count)
{
for (int i = 0; i < count; i++)
{
cout << '*';
cout << '&' << '&' << '&' << '&';
cout << '*';
}
cout << endl;
}
int main(void)
{
cout << "Magic Grid\n" << endl;
int arrayxy[6][6];
srand((unsigned)time(0));
int lowest = 1111, highest = 9999;
int range = (highest - lowest) + 1;
// Fill array with random values
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
arrayxy[i][j] = lowest + int(range*rand() / (RAND_MAX + 1.0));
}
}
// Print array as grid
for (int i = 0; i < 6; ++i)
{
WriteFrontLine(6);
for (int j = 0; j < 6; ++j)
{
cout << '*' << arrayxy[i][j] << '*';
}
cout << endl;
WriteEndLine(6);
cout << endl;
}
cout << endl;
}