C++,console, cout Vector in 2D array - c++

I am relative new to C++ so please have mercy. I am trying to cout the start and end of a "vector" in a 2D array 40*20. but the "b" dont get placed in with the last 2 for loops. In the end i want to display 2 vectors with the second starting at [0][0] and going the same direction as the one typed in by the user. I also don't know how to, get the b's to be from the starting point of the line to the end point of the line and not just start and ending point. But the main problem is that the x is not changed with the b.
Thanks for your help.
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int vectorsx;
int vectorsy;
int vectorendx;
int vectorendy;
char display[40][40];
char row;
char column;
typedef struct
{
double x;
double y;
}punkt;
typedef struct
{
punkt PA;
punkt PE;
}vector;
void query()
{
cout << "Startpunkt x:";
cin >> vectorsx;
cout << "Startpunkt y:";
cin >> vectorsy;
cout << "Endpunkt x:";
cin >> vectorendx;
cout << "Endpunkt y:";
cin >> vectorendy;
}
int main()
{
query();
vector v;
v.PA.x = vectorsx;
v.PA.y = vectorsy;
v.PE.x = vectorendx;
v.PE.y = vectorendy;
vector v2;
v2.PA.x = 0;
v2.PA.y = 0;
v2.PE.x = v.PE.x - v.PA.x;
v2.PE.y = v.PE.y - v.PA.y;
for (int row = 0; row < 20; row++)
{
for (int column = 0; column < 40; column++)
{
display[row][column] = 'x';
}
}
for (int row = 0; row < 20; row++)
{
for (int column = 0; column < 40; column++)
{
cout << display[row][column];
}
cout << endl;
}
for (row = 0; row < 20; row++)
{
for(column = 0; column < 40;column++)
{
display[vectorsx][vectorsy] = 'b';
}
}
for (row = 0; row < 20; row++)
{
for (column = 0; column < 40; column++)
{
display[vectorendx][vectorendy] = 'b';
}
}
system("Pause");
return 0;
}

You set all elements of display to 'x'. Then you print the contents of dispaly. And then you change the contents of two elements to b. You don't print the array again.
Do the printing after you change the arrays.
You also don't need the loops for the changes, you are changing the same element 800 times.

Related

Why is my loop not returning the right answer?

I can't figure it out. What is wrong with my code? I am new to programming.
Program required output: Write a C++ program to find the maximum-occurring character in an array, using a loop.
My code:
#include <string.h>
using namespace std;
void FindMaxChar(char Word[])
{
int count = 0;
int max = 0;
char index = 0;
int length = strlen(Word);
for (int i = 0; i < length; i++)
{
index = Word[i];
for (int j = 0; j < length; j++)
{
if (index == Word[j])
{
count++;
}
}
if (count > max)
{
max = count;
index = Word[i];
}
}
cout << index << " is repeating " << max << " times.";
}
int main()
{
char Word[100] = {0};
cout << "Enter the Word = ";
cin.get(Word,100);
FindMaxChar(Word);
}
My Output:
Enter the Word = caaar
r is repeating 11 times.
You never reset count each loop. So you continue incrementing it but never clear it.
Add count = 0 to the beginning of the outer for loop:
for (int i = 0; i < length; i++)
{
count = 0; // Reset counter
You're also trying to use index for two different purposes. You're both using it to store the current character you're looking at (not an index, kind of confusing that you named it like that), AND the character you've seen the most (still not an index, also confusing).
Instead, you need another variable here.
Also note that if you declare Word as char Word[100], it can only hold a c-string of length 99 (to leave room for the null character). So your cin should actually be:
cin.get(Word, 99);
Thanks to the great people of this community.
I am able to find my error and corrected it.
Correct Code :
#include <iostream>
#include <string.h>
using namespace std;
void FindMaxChar(char Word[])
{
int count = 0;
int max = 0;
char index = 0;
char final = 0;
int length = strlen(Word);
for (int i = 0; i < length; i++)
{
index = Word[i];
count = 0;
for (int j = 0; j < length; j++)
{
if (index == Word[j])
{
count++;
}
}
if (count > max)
{
max = count;
final = index;
}
}
cout << final << " is repeating " << max << " times.";
}
int main()
{
char Word[100] = {0};
cout << "Enter the Word = ";
cin.get(Word,99);
FindMaxChar(Word);
}

Passing a 2d dynamic array of strings by reference in C++

I have to use a dynamic array for this project but I can't pass it by reference, I am not sure what to do, I initialized the 2d dynamic array, but not sure how to pass it by reference.
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#define row 5
#define col 14
typedef string* StrArrPtr;
void set_up_array(string (&&layout_array)[row][col]);
int main()
{
StrArrPtr *layout_array = new StrArrPtr[col];
for(int i = 0; i < row; i++)
{
layout_array[i] = new string[col];
}
//string layout_array[row][col];
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(string (&&layout_array)[row][col])
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
I am new to c++ so the solution might be very obvious but I just can't see it. Any help would be appreciated.
Given what you are doing with the array in set_up_array, you don't need to pass the actual string* by reference, because you're simply dereferencing it (telling your computer where to look for a string) and altering the value of string located at that index. When you pass an array in C/C++, you're just passing in an int, so don't worry about references unless you're modifying what the pointer is pointing to.
(It would be like string * &ref)
To pass a 2D array by reference, use:
void set_up_array(string (&layout_array)[row][col]);
In order to be able call that function, the variable needs to be of type string [row][col].
Change main to:
int main()
{
std::string layout_array[row][col];
set_up_array(layout_array);
...
}
Since you know the sizes of the arrays at compile time, it will be better to use std::array.
#include <iostream>
#include <vector>
#include <array>
#include <string>
#include <iomanip>
using namespace std;
const int row = 5;
const int col = 14;
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array);
int main()
{
std::array<std::array<std::string, col>, row> layout_array;
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(std::array<std::array<std::string, col>, row>& layout_array)
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}
Arrays are always pass by reference.. your problem is how to pass a 2d array of string...
analyze how i did it:
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
#define row 5
#define col 14
typedef string* StrArrPtr;
void set_up_array(string* layout_array[]);
int main()
{
StrArrPtr *layout_array = new StrArrPtr[row];
for(int i = 0; i < row; i++)
{
layout_array[i] = new string[col];
}
//string layout_array[row][col];
set_up_array(layout_array);
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
if(j == 1 && i > 0)
{
cout << right << setw(11);
}
cout << "| " << layout_array[i][j] << " ";
}
cout << "|" << endl;
}
return 0;
}
void set_up_array(string* layout_array[])
{
layout_array[0][0] = "Lab Number"; //First Column / first row
layout_array[1][0] = "1"; // second row
layout_array[2][0] = "2"; // third row
layout_array[3][0] = "3"; // fourth row
layout_array[4][0] = "4"; // fifth row
}

Printing a square of zeros based off of squared integers--different from asterisks c++ [duplicate]

I have been told to ask the user to input how many rows and columns for a rectangle they would like to print and in what symbol they want it. I am unaware as to how to do this and all my google searching only got me as far as printing one row. Directions dictate that the rows should be 3 and the columns should be 7 with the character '$'. I'm still a beginner so please go easy on me. This is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
void PrintChar(int row = 5, int column = 10, char symbol = '*');
int main()
{
int rows, columns;
char symbol;
cout << "How many rows and columns do you want, and with what symbol (default is *) ?" << endl;
cin >> rows >> columns >> symbol;
PrintChar(rows, columns, symbol);
}
void PrintChar(int row, int column, char symbol)
{
for (int y = 1; y <= column; y++)
{
cout << symbol;
}
That prints out a full line of the symbol and that's where my thinking stops. If you could help me with the final rows, that would be greatly appreciated.
This should do the trick. Added a newline to make it look like a rectangle.
#include <iostream>
#include <iomanip>
using namespace std;
void PrintChar(int row = 5, int column = 10, char symbol = '*');
int main() {
int rows, columns;
char symbol;
cout << "How many rows and columns do you want, and with what symbol (default is *) ?" << endl;
cin >> rows >> columns >> symbol;
PrintChar(rows, columns, symbol);
return(0);
}
void PrintChar(int row, int column, char symbol) {
for (int y = 1; y <= column; y++) {
for (int x = 1; x <= row; x++) {
cout << symbol;
}
cout << endl;
}
}
First, int main() should have a return statement.
There should be 2 nested for loops inside PrintChar, outer one for the rows and inner one for the columns, like:-
for (int x = 1; x <= rows; x++)
{
cout << endl;
for (int y = 1; y <= columns; y++)
{
cout << symbol;
}
}
Using nested loop you can achieve that.
void PrintChar(int row, int column, char symbol)
{
for (int x = 0; x < row; x++)
{
for (int y = 1; y <= column; y++)
{
cout << symbol;
}
cout << endl;
}
}
Seems as a basic star patterns looping exercise. Use nested loops to print the required pattern
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
cout<<"*";
}
cout<<"\n";
}
Here n is the number of rows and m is number of columns each row.

display wheter or not an array entered by the user is unique or not

I am new to C++ I am making an array based off of user input and displaying whether the array is unique or not. my initial thought is I have to end up creating another array to store values and then compare the elements. My code compiles without errors but does not do what I want it to do. I can't used advanced methods such as hashmaps or vectors. Any help or insight is greatly appreciated!
#include <iostream>
#include <string>
using namespace std;
int main()
{
int myArray[6];
string name;
int i, k;
int ov = 0;
int newVal = 0;
for (int index = 0; index < 6; index++)
{
cout << "Enter number a number: ";
cin >> myArray[index];
}//end loop for
for (i = 0; i < 6; i++)
{
ov = myArray[i];
}
for (k = i + 1; k < 6; k++)
{
if (ov == myArray[k])
{
newVal = 1;
}
}
if (newVal == 1)
{
cout << "Not all unique";
}
else
{
cout << "All unique";
}
cin.get();
return 0;
}

How to print a rectangle of asterisks in C++

I have been told to ask the user to input how many rows and columns for a rectangle they would like to print and in what symbol they want it. I am unaware as to how to do this and all my google searching only got me as far as printing one row. Directions dictate that the rows should be 3 and the columns should be 7 with the character '$'. I'm still a beginner so please go easy on me. This is what I have:
#include <iostream>
#include <iomanip>
using namespace std;
void PrintChar(int row = 5, int column = 10, char symbol = '*');
int main()
{
int rows, columns;
char symbol;
cout << "How many rows and columns do you want, and with what symbol (default is *) ?" << endl;
cin >> rows >> columns >> symbol;
PrintChar(rows, columns, symbol);
}
void PrintChar(int row, int column, char symbol)
{
for (int y = 1; y <= column; y++)
{
cout << symbol;
}
That prints out a full line of the symbol and that's where my thinking stops. If you could help me with the final rows, that would be greatly appreciated.
This should do the trick. Added a newline to make it look like a rectangle.
#include <iostream>
#include <iomanip>
using namespace std;
void PrintChar(int row = 5, int column = 10, char symbol = '*');
int main() {
int rows, columns;
char symbol;
cout << "How many rows and columns do you want, and with what symbol (default is *) ?" << endl;
cin >> rows >> columns >> symbol;
PrintChar(rows, columns, symbol);
return(0);
}
void PrintChar(int row, int column, char symbol) {
for (int y = 1; y <= column; y++) {
for (int x = 1; x <= row; x++) {
cout << symbol;
}
cout << endl;
}
}
First, int main() should have a return statement.
There should be 2 nested for loops inside PrintChar, outer one for the rows and inner one for the columns, like:-
for (int x = 1; x <= rows; x++)
{
cout << endl;
for (int y = 1; y <= columns; y++)
{
cout << symbol;
}
}
Using nested loop you can achieve that.
void PrintChar(int row, int column, char symbol)
{
for (int x = 0; x < row; x++)
{
for (int y = 1; y <= column; y++)
{
cout << symbol;
}
cout << endl;
}
}
Seems as a basic star patterns looping exercise. Use nested loops to print the required pattern
for(i=1; i<=n; i++)
{
for(j=1; j<=m; j++)
{
cout<<"*";
}
cout<<"\n";
}
Here n is the number of rows and m is number of columns each row.