New Posts  All Forums:Forum Nav:

# Spiral Matrix Bug

Hello,
I'm rather new to software programming, but have solid experience in web development. As part of my major, I'm required to take courses in ANSI C. For one of our assignments, we're told to take input of two numbers: the length of a square and an initial value. Our goal is to make it so that we print all primes from the initial number to length^2 in a spiral pattern going from the center, counter clockwise.

I have the majority of the program done. It compiles without error with the tags -ansi -Wall and -pedantic. The final product prints essentially what I want, with one exception. The initial value is always printed twice. I have reviewed my code, as non-eloquent as it is, and can't seem to find the bug! Here's an example.

(This is without replacing numbers with *'s, to help it make sense. Normally non-primes would be replaced with *'s)
With length of 5 and initial value of 1
Should print:

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

Actually prints:

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

Any help in picking out this bug would be much appreciated, or at least a kick in the right direction. I'll post my source code at the bottom. I realize it's not optimized at all and doesn't look too pretty, but I've tried to make it easy to follow.

Much thanks, everyone.

EDIT: Fixed the formatting a bit, the forums don't allow tab spaces, sadly.
Code:
``````#include <stdio.h>
#include <stdlib.h>

void printRow(int row, int length, char *area);

int main(int argc, char* argv[])
{
int length, number, i, j, row;
char *area;

puts("\n\tP64: A Square Deal");
scanf("%d", &length);
area = (char*) malloc((length*length)*sizeof(char));
scanf("%d", &number);

/* Test for Primes, fill with * if not prime, otherwise fill with number */
i = 0;
while(i < (length*length))
{
area[i] = number;
if(number == 1 || number == 0)
{
area[i] = '*';
}
for(j=2;j<number;j++)
{
if(number % j == 0)
{
area[i] = '*';
}
}
number++;
i++;
}
puts("\n");

/* Start print sequence for each row of length 'length' */
for (row=0; row<length; ++row)
{
printRow(row, length, area);
printf("\n");
}

return 0;
}

void printRow(int row, int length, char *area)
{
int i;
int total = length*length;
/* Spiral runs from center out, counter clockwise. Only primes are not * */
/* First row */
if(row == 0)
{
for(i=length;i>0;i--)
{
if(area[total-(length-1)-2*length+i] == 42)
{
printf("%c\t", area[total-(length-1)-2*length+i]);
}
else
{
printf("%d\t", area[total-(length-1)-2*length+i]);
}
}
}

/* Last row */
if(row == length-1)
{
for(i=0;i<length;i++)
{
if(area[total+i-length] == 42)
{
printf("%c\t", area[total+i-length]);
}
else
{
printf("%d\t", area[total+i-length]);
}
}
}

/* Middle rows */
/* First and second printfs are the sides, recursion portion is the inside numbers */
if(row != length-1 && row != 0)
{
if(area[total-2*length+row+1] == 42 || area[total-3*length-row+2] == 42)
{
printf("%c\t", area[total-2*length+row+1]);
printRow(row-1, length-2, area);
printf("%c\t", area[total-3*length-row+2]);
}
else
{
printf("%d\t", area[total-2*length+row+1]);
printRow(row-1, length-2, area);
printf("%d\t", area[total-3*length-row+2]);
}
}
}```
```

Edited by PapayaJuice - 12/7/11 at 7:19pm
When I ran that in a C++ app i get a "☻"
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
Quote:
Originally Posted by FiX

When I ran that in a C++ app i get a "☻"

Yes, the examples I gave were if printed without replacing non-primes with *'s. When I print with the *'s, that middle row is printed as:

19 * * * ☻ 11
Quote:
P64: A Square Deal

5

1

17 * * * 13
* 5 * 3 *
19 * * * ☻ 11
* 7 * * *
* * 23 * *
Press any key to continue . . .
This sound OK?
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
That's what it outputs currently. The problem is in that middle line:
19 * * * ☻ 11

When not 'hidden' but the *'s, that read 19 6 1 1 2 11.

The problem is that 1 showing up twice(explains the extra *) and the 2 becoming the ☻because of it.
Code makes more sense when its not hiding numbers
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
Yeah...sadly, that's what he wants us to do. Want me to comment that part out and repost it? I know my code isn't the easiest to read.
Its happening when you get a total of 1 plus i of 0 minus length of 1. area[0] is that unusual character.
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
Hmmmm, alright. So I have to dodge the area[0]? I'll attempt it and report back!
It work?
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
 Phenomenon - no longer a phenom... (20 items)
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D
Audio
Asus Xonar Essence STX
New Posts  All Forums:Forum Nav:
Return Home
Back to Forum: Application Programming