Question

In: Computer Science

C++ program please, can you show me where I did wrong. I'm trying to print the...

C++ program please, can you show me where I did wrong. I'm trying to print the counter-clockwise spiral form using int *p, but my output turned out weird, thank you

void makeSpiral(int *p, int rows, int cols)
{
    int left = 0, value = 1, top = 0;
    while(left < cols && top < rows)
    {
         for(int i = top;i < rows;++i)
         {
             *(p+i*cols+left) = value++;
         }
         left++;
         for(int i = left;i < cols;++i)
         {
             *(p+(rows-1)*cols+i) = value++;
         }
         rows--;
         if (top < rows)
         {
             for(int i = rows-1; i >= top; --i)
             {
                 *(p + i * cols + (cols-1)) = value++;
             }
             cols--;
         }
         if (left < cols)
         {
             for(int i = cols - 1 ;i >= left; --i)
             {
                 *(p + top * cols + i) = value++;
             }
             top++;
         }
     }
}

My output:

---------------------------------------------
1
---------------------------------------------
1 4
2 3
---------------------------------------------
1 8 7
9 0 6
3 4 5
---------------------------------------------
1 12 11 10
13 16 0 14
15 0 0 8
4 5 6 7
---------------------------------------------
1 16 15 14 13
24 0 23 25 18
3 22 0 19 20
21 0 0 0 10
5 6 7 8 9
---------------------------------------------
1 18 17 16 15 14 13
28 27 26 0 25 0 20
21 22 23 24 0 0 11
4 5 6 7 8 9 10
---------------------------------------------
1 18 17 16
19 28 0 20
27 0 21 26
4 22 25 13
23 24 0 12
6 0 0 11
7 8 9 10

The output that I supposed to have:

---------------------------------
1
---------------------------------
1 3
---------------------------------
3
2
---------------------------------
1 4
2 3
---------------------------------
1 8 7
2 9 6
3 4 5
---------------------------------
1 12 11 10
2 13 16 9
3 14 15 8
4 5 6 7
---------------------------------
1 16 15 14 13
2 17 24 23 12
3 18 25 22 11
4 19 20 21 10
5 6 7 8 9

Solutions

Expert Solution


//Write c++ program to print the counter-clockwise spiral
#include<iostream.h>
#include<conio.h>
#define R 3
#define C 3
void makeSpiral(int m, int n, int matrix[R][C])
{
int i, k = 0, l = 0;
int count = 0;
int total = m * n;
while (k < m && l < n){
if (count == total)
break;
for (i = k; i < m; ++i){
cout<<matrix[i][l]<<" ";
count++;
}
l++;
if (count == total)
break;
for (i = l; i < n; ++i){
cout<<matrix[m - 1][i]<<" ";
count++;
}
m--;
if (count == total)
break;
if (k < m){
for (i = m - 1; i >= k; --i){
cout<<matrix[i][n - 1]<<" ";
count++;
}
n--;
}
if (count == total)
break;
if (l < n){
for (i = n - 1; i >= l; --i){
cout<<matrix[k][i]<<" ";
count++;
}
k++;
}
}
}
void main()
{
int mat[R][C] =
{
  
{1, 16 ,15 ,14, 13},
{2, 17 ,24 ,23 ,12},
{3, 18, 25, 22, 11},
{4 ,19 ,20, 21, 10},
{5, 6, 7, 8, 9}
};
cout<<"Conter Clockwise Spiral from of the matrix is :\n";
makeSpiral(R, C, mat);
getch();
}

Output:


Related Solutions

Hi, the person who answered this before did it wrong. Can you please show me the...
Hi, the person who answered this before did it wrong. Can you please show me the formulas of how to answer this. Hi-Tek Manufacturing Inc. makes two types of industrial component parts—the B300 and the T500. An absorption costing income statement for the most recent period is shown below: Hi-Tek Manufacturing Inc. Income Statement Sales $ 1,718,000 Cost of goods sold 1,220,369 Gross margin 497,631 Selling and administrative expenses 650,000 Net operating loss $ (152,369) Hi-Tek produced and sold 60,300...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
Can you please modify where I'm going wrong (regarding Problem #2 I posted earlier; i.e., credit...
Can you please modify where I'm going wrong (regarding Problem #2 I posted earlier; i.e., credit card validation)? Please fix my code using the general format that reflects my current level of coding/where I am in the learning process: import java.util.Scanner; public class AmExValidator{ public static void main(String[] args) { Scanner amex = new Scanner(System.in); System.out.print("Enter credit card number for validation: "); String ccNum = amex.nextString(); //cc = credit card number as a string (not #) //checking for cc length...
Can you please check my answers and if I am wrong correct me. Thank you! A....
Can you please check my answers and if I am wrong correct me. Thank you! A. In today's interconnected world, many central banks communicate regularly and frequently with the public about the state of the economy, the economic outlook, and the likely future course of monetary policy. Communication about the likely future course of monetary policy is known as "forward guidance.". If the central bank increases the reserve ratio, as the market has perfectly expected, which of the following will...
I'm trying to make this C++ loan calculator but I can't get the program to output...
I'm trying to make this C++ loan calculator but I can't get the program to output what I need. For instance I know the formula is right and when I enter 100000 1.5 20 as my cin variables I should get 482.55 but I get 1543.31. What am I not seeing as the issue? Also this should cout only 2 decimal places right? I'm not allowed to alter the #include and add any fixed setprecision. This is what I have....
Can you please show me the formulas you used in these problems? I keep using the...
Can you please show me the formulas you used in these problems? I keep using the wrong ones. In his last ride in a rocket sled in 1955, Col. Stapp was accelerated to 632 miles per hour in 5.0 s, and then brought to a stop with an acceleration of 46.2 g, the largest acceleration voluntarily endured by a human. a. How far did the sled move in the first phase, as it accelerated to maximum speed? b. How far...
Can you please see what I have done wrong with my program code and explain, This...
Can you please see what I have done wrong with my program code and explain, This python program is a guess my number program. I can not figure out what I have done wrong. When you enter a letter into the program, its supposed to say "Numbers Only" as a response. I can not seem to figure it out.. instead of an error message. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if...
can you please create the code program in PYTHON for me. i want to create array...
can you please create the code program in PYTHON for me. i want to create array matrix Nx1 (N is multiple of 4 and start from 16), and matrix has the value of elements like this: if N = 16, matrix is [ 4 4 4 4 -4 -4 -4 -4 4 4 4 4 -4 -4 -4 -4] if N = 64, matrix is [8 8 8 8 8 8 8 8 -8 -8 -8 -8 -8 -8 -8...
Can you fix to me this code plz I just want to print this method as...
Can you fix to me this code plz I just want to print this method as the reverse. the problem is not printing reverse. public void printBackward() {               Node curr = head;        Node prev = null;        Node next = null;               System.out.print("\nthe backward of the linkedlist is: ");               while(curr != null) {            next = curr.next;            curr.next = prev;   ...
I am trying to create a PICO question research question. Can you please give me some...
I am trying to create a PICO question research question. Can you please give me some examples that i can use? At least 3 PICO questions and their research question and what do you want to find out about the issue. Thank you
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT