I need help, I created a short little program a while ago where it would print a simple pyramid with "*" like this:
*
***
*****
but I decided to challenge myself and see if I could create a simple diamond shape like this:
*
***
*****
***
*
Here is my code so far.
I should also add that the value you input, for example 5, determines how big the diamond is.
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value; i++) {
int number = 0;
number+= 2;
//print spaces v v v
for (int x = 0; x < (value - value + i + 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (/*ATTENTION: What do I do here? Plz help*/); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
What I've been trying to do is figure out what to put inside the parenthesis where it says (//ATTENTION). I've been working for at least an hour trying to do random things, and one time it worked when I input 4, but not for 5, and it's just been very hard. This is key to building the diamond, try putting in just value and compile to see what happens. I need it to be symmetrical.
I need to know what to put inside the parenthesis please. I'm sorry this is very long but the help would be appreciated thanks.
I also apologize if my code is messy and hard to read.
int number = 0; and number+= 2;
value - value inside for (int x = 0; x < (value - value + i + 1); x++) {
are not required.
Inside the parenthesis, you can use
2*(value-i-1)-1
However, I would suggest you to first analyze the problem and then try to solve it instead of trying random things. For instance, let's consider the cases of even and odd inputs i.e., 2 and 3.
Even Case (2)
*
***
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 1 1
1 0 3
2 0 3
3 1 1
For row index < value
Number of Spaces = value - row index - 1
Number of Stars = 2 * row index + 1
For row index >=value
The number of spaces and stars are simply reversed. In the odd cases, the situation is similar too with a small exception.
Odd Case (3)
*
***
*****
***
*
The Analysis
Row Index Number of Spaces Number of Stars
0 2 1
1 1 3
2 0 5
3 1 3
4 2 1
The small exception is that while reversing, we have to ignore the row index = value.
Now, if we put the above analysis in code we get the solution
//Define the Print Function
void PrintDiamond(int rowIndex, int value)
{
//print spaces v v v
for (int x = 0; x < value - rowIndex -1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < 2 * rowIndex + 1; y++) {
cout << "*";
}
cout << endl;
}
And then inside main
//Row index < value
for (int i = 0; i < value; i++) {
PrintDiamond(i,value);
}
//For row index >= value reversing the above case
//value-(value%2)-1 subtracts 1 for even and 2 for odd cases
//ignore the row index = value in odd cases
for (int i = value-(value%2)-1; i >=0; i--) {
PrintDiamond(i,value);
}
#include <iostream>
#include <sstream>
using namespace std;
int main() {
int value = 0;
cout << "Please enter in a value: ";
cin >> value;
cout << endl;
for (int i = 0; i < value; i++) {
//print spaces v v v
for (int x = 0; x < (value - i - 1); x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2 * i + 1); y++) {
cout << "*";
}
cout << endl;
}
for (int i = 0; i < value-1; i++) {
// int number = 0;
// number+= 2;
// //print spaces v v v
for (int x = 0; x < i+1; x++) {
cout << " ";
}
//print * v v v
for (int y = 0; y < (2*(value-1-i)-1); y++) {
cout << "*";
}
cout << endl;
}
return 0;
}
I hope that you will get this .Also in the second for loop you were iterating it one extra time by iterating the loop upto value. But since the pyramid is symmetric so the no of rows in the pyramid will be 2*value-1.So I in the second loop i should vary upto value -1.
This code should resolve the problem:
#include <sstream>
using namespace std;
void printSpaces(int howMany) {
for(int i = 0; i < howMany; i++) cout << " ";
}
void figure(int size) {
bool oddSize = size % 2 == 1;
int center = size / 2;
int spaces = size / 2;
// If figure is of an odd size adjust center
if (oddSize) {
center++;
} else { // Else if figure is of even size adjust spaces
spaces--;
}
for (int i = 1; i <= center; i++) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++) cout << "*";
cout << endl;
spaces--;
}
spaces = oddSize ? 1 : 0; // If the figure's size is odd number adjust spaces to 1
center -= oddSize ? 1 : 0; // Adjust center if it's an odd size figure
for(int i = center; i >= 1; i--) {
printSpaces(spaces);
for(int j = 0; j < 1 + (i - 1) * 2; j++)
cout << "*";
cout << endl;
spaces++;
}
}
int main() {
int value = 0;
while(value < 3) {
cout << "Please enter in a value (>= 3): ";
cin >> value;
cout << endl;
}
figure(value);
return 0;
}
Related
The idea I have is pretty simple. What I'd like to do is just have the user input a series of integers and plug those integers specified into an array. So for example, here's a segment of the program I'm writing.
const int size = 1000;
int array_one[size], array_two[size];
for (int i = 0; i < size; i++)
{
std::cin >> array_one[i];
if (array_one[i] == -1) break;
}
for (int i = 0; i < size; i++)
{
std::cin >> array_two[i];
if (array_two[i] == -1) break;
}
The cut off point is -1. Should the user enter this, one index of the array will be all integers greater than -1 with the following indexes housing 0's in this 1000 sized array.
What I'm doing next is simply replicating how you would write out basic addition on paper. So, lets say for example I plugged in 1 0 0 6 -1 as inputs for the first array and then 2 3 4 for the second array. The whole entire output should look something akin to this
1006
+ 234
------
1240
Here's what I've typed out to get this sort of output
int array_size_one = 0;
int array_size_two = 0;
int space_size = 3;
int cut_off_space = 0;
int count = 0;
for (int i = 0; i < size; ++i)
{
if (array_one[i] < 0) break;
array_size_one += 1;
}
for (int i = 0; i < size; ++i)
{
if (array_two[i] < 0) break;
array_size_two += 1;
}
if (array_size_one > array_size_two) space_size += array_size_one;
else if (array_size_two > array_size_one) space_size += array_size_two;
if (array_size_one < array_size_two) cut_off_space += array_size_one;
else if (array_size_two < array_size_one) cut_off_space += array_size_two;
std::cout << std::setw(space_size - array_size_one);
for (int j = 0; j < size; ++j)
{
if (array_one[j] < 0) break;
std::cout << array_one[j];
}
std::cout << "\n+";
std::cout << std::setw((space_size - 1) - array_size_two);
for (int j = 0; j < size; ++j)
{
if (array_two[j] < 0) break;
std::cout << array_two[j];
}
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
This is the part where I think I'm sure I'm having trouble with.
std::cout << "\n";
std::cout << std::setfill('-') << std::setw(space_size) << " " << std::endl;
for (int i = 0; i < (space_size - 3); ++i)
{
count = array_one[i] + array_two[i];
}
std::cout << count << std::endl;
return 0;
The idea was that I wanted to add up everything inside each index of the arrays to perform standard integer expressions. The for loop above isn't really cutting it. I'm getting outputs like 5 or 15. If going by the example above that has inputs of 1 0 0 6 -1 for the first array and then 2 3 4 -1 for the second array. What could I actually do inside the very last for loop iteration to make sure I get the correct outputs replicating standard integer expressions?
I'm working on a code that finds all saddle points in a matrix. Both smallest in their row and biggest in their column, and biggest in their row and smallest in their column fall under the definition (of my university) of a saddle point. Being a beginner I managed to get half of it done (finding saddle points which are smallest in their row and biggest in their column) by copying parts of what we've done in class and typing it myself. I have been stuck on it for quite some time and can't figure how to add the saddle points which are biggest in their row and smallest in their column to the program.
This is what I have so far:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int x, y;
int pos_max(int j) //saddle points check
{
int max = 0;
for (int i = 1; i <= x - 1; i++) {
if (a[i][j] > a[max][j]) {
max = i;
}
}
return max;
}
int main() {
cout << "Enter the number of rows: ";
cin >> x;
cout << "Enter the number of columns: ";
cin >> y;
cout << "----------------------------" << endl;
for (int i = 0; i <= x - 1; i++) //input of the matrix
for (int j = 0; j <= y - 1; j++) {
cout << "a[" << i + 1 << ", " << j + 1 << "] = ";
cin >> a[i][j];
}
cout << "----------------------------\n";
for (int i = 0; i <= x - 1; i++) //visualization of the matrix
{
for (int j = 0; j <= y - 1; j++)
cout << a[i][j] << " ";
cout << endl;
}
cout << "----------------------------\n";
int r;
int flag = 0;
int i = y;
for (int j = 0; j <= y - 1; j++) {
r = pos_max(j);
for (i = 0; i <= y - 1; i++) {
if (a[r][i] < a[r][j]) {
break;
}
}
if (i == y) {
cout << "Saddle points are: ";
cout << "a[" << r + 1 << ", " << j + 1 << "] = " << a[r][j] << "\n";
flag = 1;
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
First, there is a logical error with your code. In the pos_max function, it will return the index of the element which is maximum in the column. There can be a case when there are multiple maximum with the same value in the column, however, it returns the one which is not the minimum in the row, hence your program won't be able to print that saddle point.
To solve this, you can either return an array of all indices which are maximum in a column and then check for each of those points if it's minimum in their respective column, but I think it's not a very elegant solution. In any case, you will again have to write the entire code for the other condition for saddle points, minimum in column and maximum in row.
Hence, I would suggest a change in strategy. You create 4 arrays, max_row, max_col, min_row, min_col, where each array stores the minimum / maximum in that row / column respectively. Then you can traverse the array and check if that point satisfies saddle point condition.
Here is the code:
#include <iostream>
#include <cstdlib>
using namespace std;
int a[10][10];
int max_row[10], max_col[10], min_row[10], min_col[10];
int x, y;
bool is_saddle(int i, int j) {
int x = a[i][j];
return (max_row[i] == x && min_col[j] == x) || (min_row[i] == x && max_col[j] == x);
}
int main() {
/* code to input x, y and the matrix
...
*/
/* code to visualize the matrix
...
*/
/* populating max and min arrays */
for (int i = 0; i <= x-1; ++i) {
max_row[i] = a[i][0], min_row[i] = a[i][0];
for (int j = 0; j <= y-1; ++j) {
max_row[i] = max(max_row[i], a[i][j]);
min_row[i] = min(min_row[i], a[i][j]);
}
}
for (int j = 0; j <= y-1; ++j) {
max_col[j] = a[0][j], min_col[j] = a[0][j];
for (int i = 0; i <= x-1; ++i) {
max_col[j] = max(max_col[j], a[i][j]);
min_col[j] = min(min_col[j], a[i][j]);
}
}
/* Check for saddle point */
for (int i = 0; i <= x-1; ++i) {
for (int j = 0; j <= y-1; ++j) {
if (is_saddle(i, j)) {
cout << "Saddle points are: ";
cout << "a[" << i + 1 << ", " << j + 1 << "] = " << a[i][j] << "\n";
flag = 1;
}
}
}
if (flag == 0) {
cout << "No saddle points\n";
}
cout << "----------------------------\n";
return 0;
}
#include <iostream>
using namespace std;
int getMaxInRow(int[][5], int, int, int);
int getMinInColumn(int[][5], int, int, int);
void getSaddlePointCordinates(int [][5],int ,int );
void getInputOf2dArray(int a[][5], int, int);
int main()
{
int a[5][5] ;
int rows, columns;
cin >> rows >> columns;
getInputOf2dArray(a, 5, 5);
getSaddlePointCordinates(a,rows,columns);
}
void getInputOf2dArray(int a[][5], int rows, int columns)
{
for (int i = 0; i < rows; i = i + 1)
{
for (int j = 0; j < columns; j = j + 1)
{
cin >> a[i][j];
}
}
}
void getSaddlePointCordinates(int a[][5],int rows,int columns)
{
int flag = 0;
for (int rowNo = 0; rowNo < 5; rowNo++)
{
for (int columnNo = 0; columnNo < 5; columnNo++)
{
if (getMaxInRow(a, rows, columns, rowNo) == getMinInColumn(a, rows, columns, columnNo))
{
flag = 1;
cout << rowNo << columnNo;
}
}
}
if (flag == 0)
cout << "no saddle point";
cout << "\n";
}
int getMaxInRow(int a[][5], int row, int column, int rowNo)
{
int max = a[rowNo][0];
for (int i = 1; i < column; i = i + 1)
{
if (a[rowNo][i] > max)
max = a[rowNo][i];
}
return max;
}
int getMinInColumn(int a[][5], int row, int column, int columnNo)
{
int min = a[0][columnNo];
for (int i = 1; i < row; i = i + 1)
{
if (a[i][columnNo] < min)
min = a[i][columnNo];
}
return min;
}
just take the reference arr(ref[size]) // memorization method to check the minimum and maximum value in it.
Here is the Code Implementation with time complexity O(n *n) & space complexity O(n):
#include <bits/stdc++.h>
using namespace std;
#define size 5
void util(int arr[size][size], int *count)
{
int ref[size]; // array to hold all the max values of row's.
for(int r = 0; r < size; r++)
{
int max_row_val = arr[r][0];
for(int c = 1; c < size; c++)
{
if(max_row_val < arr[r][c])
max_row_val = arr[r][c];
}
ref[r] = max_row_val;
}
for(int c = 0; c < size; c++)
{
int min_col_val = arr[0][c];
for(int r = 1; r < size; r++) // min_val of the column
{
if(min_col_val > arr[r][c])
min_col_val = arr[r][c];
}
for(int r = 0; r < size; r++) // now search if the min_val of col and the ref[r] is same and the position is same, if both matches then print.
{
if(min_col_val == ref[r] && min_col_val == arr[r][c])
{
*count += 1;
if((*count) == 1)
cout << "The cordinate's are: \n";
cout << "(" << r << "," << c << ")" << endl;
}
}
}
}
// Driver function
int main()
{
int arr[size][size];
for(int i = 0; i < size; i++)
{
for(int j = 0; j < size; j++)
cin >> arr[i][j];
}
int count = 0;
util(arr, &count);
if(!count)
cout << "No saddle points" << endl;
}
// Test case -> Saddle Point
/*
Input1:
1 2 3 4 5
6 7 8 9 10
1 2 3 4 5
6 7 8 9 10
0 2 3 4 5
Output1:
The cordinate's are:
(0,4)
(2,4)
(4,4)
Input2:
1 2 3 4 5
6 7 8 9 1
10 11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
Output2:
No saddle points
*/
I am currently working on a task which says the following:
Input a two-dimensional array A (m,n) [m < 10, n < 20]. In the n + 1 column calculate the sum of the rows, and in the m + 1 row the product of the columns. Print out the resulting matrix.
According to my understanding of this task, at the end of each column must be the sum of according rows (so on the right hand side), and the product of the column (at the end/bottom?).
This task is so confusing I do not know where to start. I found some code that covers the idea but does not include the product and it does not display these values as the task asks me to:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[3][3];
int i, j, s = 0, sum = 0;
cout << "Enter 9 elements of 3*3 Matrix \n";
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
cin >> a[i][j];
cout << "Matrix Entered By you is \n";
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
cout << a[i][j] << " ";
cout << endl;
}
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
s = s + a[i][j];
cout << "sum of" << i + 1 << " Row is" << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
s = s + a[j][i];
cout << "sum of" << i + 1 << " Column is" << s;
s = 0;
cout << endl;
}
cout << endl;
for (i = 0; i < 3; i++)
sum = sum + a[i][i];
cout << "Sum of Diagnols Elements is \n" << sum;
getch();
}
We beginners should help each other.
Here you are
#include <iostream>
#include <iomanip>
int main()
{
const size_t M = 10;
const size_t N = 20;
int a[M][N] = {};
std::cout << "Enter number of rows: (less than " << M << "): ";
size_t m;
std::cin >> m;
if (!(m < M) || (m == 0)) m = M - 1;
std::cout << "Enter number of columns: (less than " << N << "): ";
size_t n;
std::cin >> n;
if (!(n < N) || (n == 0)) n = N - 1;
std::cout << std::endl;
for (size_t i = 0; i < m; i++)
{
std::cout << "Enter " << n
<< " numbers for the row " << i << ": ";
for (size_t j = 0; j < n; j++) std::cin >> a[i][j];
}
for (size_t j = 0; j < n; j++) a[m][j] = 1;
for (size_t i = 0; i < m; i++)
{
for (size_t j = 0; j < n; j++)
{
a[i][n] += a[i][j];
a[m][j] *= a[i][j];
}
}
std::cout << std::endl;
for (size_t i = 0; i < m + 1; i++)
{
for (size_t j = 0; j < n + 1; j++)
{
std::cout << std::setw(3) << a[i][j] << ' ';
}
std::cout << '\n';
}
std::cout << std::endl;
}
The program output might look like
Enter number of rows: (less than 10): 3
Enter number of columns: (less than 20): 3
Enter 3 numbers for the row 0: 1 2 3
Enter 3 numbers for the row 1: 4 5 6
Enter 3 numbers for the row 2: 7 8 9
1 2 3 6
4 5 6 15
7 8 9 24
28 80 162 0
So you have to declare an array with 10 rows and 20 columns. The user should enter the dimensions of the array that are correspondingly less than 10 and 20. One row and one column are reserved for sums and products.
It is desirable that the array would be initially initialized by zeroes.
int a[M][N] = {};
In this case you need not to set the last column with zeroes as you have to do with last row initializing it with ones.
That is all.:)
Start with the declaration: make sure that your program works with m×n matrix, not simply a 3×3 matrix. Since m and n have limits of 10 and 20, and because you must add an extra row and a column to the result, the declaration should be
int a[11][21];
You also need to declare m and n, have end-user enter them, and validate them to be within their acceptable ranges:
int m, n;
cin >> m >> n;
... // Do the validation
Now you can rewrite your loops in terms of m and n, rather than using 3 throughout your code.
With these declarations in place, you can total the numbers in place, i.e. for each row r you would write
for (int i = 0 ; i != n ; i++) {
a[r][n+1] += a[r][i];
}
Similarly, you would compute the product (don't forget to start it with the initial value of 1, not 0).
At the end you would print an (m+1)×(n+1) matrix to complete the task.
Solution : use this kind of functions for your array after making it global.
void Adder(int row, int colum)
{
for (int i = 0; i < row - 1; i++)
{
int temp = 0;
for (int j = 0; j < colum - 1; j++)
{
temp += a[i][j]; // added all other than last one
}
a[i][j] = temp; // assigned to last one in row
}
}
void Mul(int row, int colum)
{
for (int i = 0; i < colum- 1; i++)
{
int temp = 1;
for (int j = 0; j < row - 1; j++)
{
temp *= a[j][i]; // multiplied all element other than last one
}
a[j][i] = temp; // assigned to last one in column
}
}
I'm trying to create a program in c++ in which takes the users input and displays that specific row of Pascal's triangle.
i.e. inputting 3 should result in 1 3 3 1.
However when I input 3 in my program it displays "1 1 1 1". I think I'm fairly close but I can't quite work out what's wrong.
#include <iostream>
using namespace std;
int n, k, fact1, fact2, fact3, number1, number2, number3, nTakeAwayK, nChooseK;
void CalculateNChooseK();
int main()
{
cout << "Enter a row number for Pascal's Triangle: ";
cin >> n;
int x = 1;
for (int k = 0; k <= n; k++)
{
cout << x << '\t';
CalculateNChooseK();
x = nChooseK;
}
cout << endl;
system("PAUSE");
return 0;
}
void CalculateNChooseK()
{
fact1 = 1;
for (number1 = 1; number1 <= n; number1++)
{
fact1 = (fact1 * number1);
}
fact2 = 1;
for (number2 = 1; number2 <= k; number2++)
{
fact2 = (fact2 * number2);
}
nTakeAwayK = (n - k);
fact3 = 1;
for (number3 = 1; number3 <= nTakeAwayK; number3++)
{
fact3 = (fact3 * number3);
}
nChooseK = fact1 / (fact2*fact3);
}
Modify your code to this.. Hope this will help
for (k = 1; k <= n+1; k++)
{
cout << x << '\t';
CalculateNChooseK();
x = nChooseK;
}
As you are declaring k in for loop, This value is not working as global.
for (int k = 0; k <= n; k++) // don't declare k here.. and also loop from 1 to n+1 inclusive
I was wondering does anyone know how to convert a string into a 2d array? This was my attempt:
string w;
char s[9][9];
int p=0;
getline(cin, w);
while(p != w.size())
{
for (int k = 0; k < 9; k++)
{
for(int j = 0; j < 9; j++)
{
s[k][j] = w[p];
p++;
}
}
}
cout << "nums are: " << endl;
for(int k = 0; k < 9; k++)
{
for(int j = 0; j <9; j++)
{
cout << s[k][j];
}
}
But the numbers don't print out correctly. I want s[k][j] to print out everything in w but it simply prints out gibberish. I also noticed if i do string[81] then I get a whole bunch of errors. Could anyone help me? Thanks.
Try this:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
exit(1);
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
}
}
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
Based on our chat, you might want this:
const int NUM_ROWS = 9;
const int NUM_COLS = 9;
string w;
char s[NUM_ROWS][NUM_COLS];
while (!cin.eof())
{
bool bad_input = false;
getline(cin, w);
if (w.size() != (NUM_ROWS * NUM_COLS))
{
cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
continue;
}
for (int count = 0; count < w.size(); count++)
{
if (!isdigit(w[count]) && w[count] != '.')
{
cerr << "The character at " << count << " is not a number!" << endl;
bad_input = true;
break;
}
}
if (bad_input)
continue;
for (int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
s[row][col] = w[col + (row * NUM_COLS)];
}
}
cout << "Nums are: " << endl;
for(int row = 0; row < NUM_ROWS; row++)
{
for(int col = 0; col < NUM_COLS; col++)
{
cout << s[row][col] << " ";
}
cout << endl;
}
}
You haven't described what you're trying to do very well and you haven't described the problem you're encountering, so the following is just based on guesses.
So it looks what you're trying to do is take a string like:
The quick brown fox jumped over the lazy dogs.
And put that into a 2D array like:
0 1 2 3 4 5 6 7 8
0 T h e q u i c k
1 b r o w n f o
2 x j u m p e d
3 o v e r t h e
4 l a z y d o g s
5 . x x x x x x x x
6 x x x x x x x x x
7 x x x x x x x x x
8 x x x x x x x x x
One thing wrong with your code is that as you copy values from w into s you don't ensure that the index p is actually within the bounds. You seem to have attempted to deal with this in the line that says while(p != w.size()); but that's an outer loop that does not protect p from being incremented out of bounds and used in the inner loops. Instead you'd have to put something like p++; if (p==w.size()) break; inside the inner most loop where you increment p. Or better yet, you should iterate over the string instead of over the array. Something like the following pseudo-code would replace your entire while(p){for(k){for(j){}}} set of loops.:
for(size_t i=0; i<w.size(); ++i) {
int k = compute target row from i
int j = compute target column from i
s[k][j] = w[i]
}
Also, here's some code to better visualize the array as you're debugging.
#include <iostream>
int main() {
char s[9][9] = {"The","quick","brown","fox","jumped","over","the","lazy","dogs."};
// your code to get input and copy it into the array goes here
//
// for(size_t i=0; i<w.size(); ++i) {
// int k = compute target row from i
// int j = compute target column from i
// s[k][j] = w[i]
// }
std::cout << " 0 1 2 3 4 5 6 7 8\n";
for (int i=0; i<9; ++i) {
std::cout << i;
for (int j=0; j<9; ++j)
std::cout << ' ' << s[i][j];
std::cout << '\n';
}
}
If you run this program without any changes the output should look like this:
0 1 2 3 4 5 6 7 8
0 T h e
1 q u i c k
2 b r o w n
3 f o x
4 j u m p e d
5 o v e r
6 t h e
7 l a z y
8 d o g s .