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 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...
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
please show me or tell me a trick, on how can i tell right away when...
please show me or tell me a trick, on how can i tell right away when a characteristics equation(system) is 1)overdamped 2)underdamped 3)critically damped 4) nonlinear show each an example write neatly!!!!!
Please show how the solution was arrived at I'm trying to understand how to do the...
Please show how the solution was arrived at I'm trying to understand how to do the problem. 4.(2) The fern is defined as the unit of force required to accelerate a unit of mass, called the bung, with the gravitational acceleration on the surface of the moon, which is one-sixth the normal gravitational acceleration on earth. a) What is the conversion factor that would be used to convert a force from the natural unit to the derived unit in this...
(PLEASE SHOW STEPS TO SOLUTIONS. I'M GETTING STUCK IN MY PROCESS AND NOT SURE WHERE I'M...
(PLEASE SHOW STEPS TO SOLUTIONS. I'M GETTING STUCK IN MY PROCESS AND NOT SURE WHERE I'M GOING WRONG AND WHAT TO DO NEXT.) Skycell, a major European cell phone manufacturer, is making production plans for the coming year. Skycell has worked with its customers (the service providers) to come up with forecasts of monthly requirements (in thousands of phones) as shown in the table below (e.g., demand in Jan. is 1,000,000 units.) Manufacturing is primarily an assembly operation, and capacity...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to work on abstract base classes... not sure what is going on. import math from abc import ABC from abc import abstractmethod #TODO: convert this to an ABC class Shape(ABC): def __init__(self): self.name = "" def display(self): print("{} - {:.2f}".format(self.name, self.get_area())) #TODO: Add an abstractmethod here called get_area @abstractmethod def get_area(self): if self.name == "Circle": get_area()= 3.14 * radius * radius else: get_area() = self.length...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT