9 Replies - 1010 Views - Last Post: 01 February 2012 - 07:17 PM Rate Topic: -----

#1 EveilAxe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 01-March 10

problem on swapping Array in C

Posted 01 February 2012 - 03:04 PM

I'm trying to create a mirror image of a picture using 3D array. And I ran in to some error on my program that I don't know how to fix it. The following is my array swap code. And when I run it there is an error on "tempt = img[i][j][RED];" Can anyone help me to fix this error?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "bitmap.h"
#include <pshpack2.h>


void swap (int ***img , int rows , int cols)
{
	int trows = rows; //tempt rows
	int tcols = cols; //tempt cols
	int tempt = 0;
	for(int i=0; i < trows /2 ; i++)
	{
		for(int j=0; j< tcols /2; j++)
		{
			//swap
			tempt = img[i][j][RED]; //Error on this line.
			img[i][j][RED] = img[rows][cols][RED];
			img[rows][cols][RED] = tempt;

			tempt = img[i][j][GRN];
			img[i][j][GRN] = img[rows][cols][GRN];
			img[rows][cols][GRN] = tempt;

			tempt = img[i][j][BLU];
			img[i][j][BLU] = img[rows][cols][BLU];
			img[rows][cols][BLU] = tempt;

			cols--;
			rows--;
		}
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem on swapping Array in C

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: problem on swapping Array in C

Posted 01 February 2012 - 03:08 PM

Please post the complete error message exactly as it appears in your development environment. These messages have important information imbedded within them.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 EveilAxe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 01-March 10

Re: problem on swapping Array in C

Posted 01 February 2012 - 03:23 PM

Actually, there are really no error msg but it just says that the program stopped there. It says some thing like "The Lab Exercis 2.exe at 0x00102247 unhandled exception occurred: 0xC0000005: read access violation occurs when the location 0xfdfe03fd"
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: problem on swapping Array in C

Posted 01 February 2012 - 03:38 PM

Sounds like you're trying to access memory that isn't a part of your program's memory. Meaning, you are more than likely trying to access outside of the bounds of the array.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: problem on swapping Array in C

Posted 01 February 2012 - 04:22 PM

If I have an array with 10 elements then it is indexed 0 though 9 -- that is array[maxsize-1] is the last element in the array.

if I have iRow rows and iCol Columns then array[iRow][iCol] is not within the array, I probably mean to access: array[iRow-1][iCol-1]
Was This Post Helpful? 0
  • +
  • -

#6 Karel-Lodewijk   User is offline

  • D.I.C Addict
  • member icon

Reputation: 455
  • View blog
  • Posts: 864
  • Joined: 17-March 11

Re: problem on swapping Array in C

Posted 01 February 2012 - 04:28 PM

View PostEveilAxe, on 01 February 2012 - 10:04 PM, said:

I'm trying to create a mirror image of a picture using 3D array. And I ran in to some error on my program that I don't know how to fix it. The following is my array swap code. And when I run it there is an error on "tempt = img[i][j][RED];" Can anyone help me to fix this error?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include "bitmap.h"
#include <pshpack2.h>


void swap (int ***img , int rows , int cols)
{
	int trows = rows; //tempt rows
	int tcols = cols; //tempt cols
	int tempt = 0;
	for(int i=0; i < trows /2 ; i++)
	{
		for(int j=0; j< tcols /2; j++)
		{
			//swap
			tempt = img[i][j][RED]; //Error on this line.
			img[i][j][RED] = img[rows][cols][RED];
			img[rows][cols][RED] = tempt;

			tempt = img[i][j][GRN];
			img[i][j][GRN] = img[rows][cols][GRN];
			img[rows][cols][GRN] = tempt;

			tempt = img[i][j][BLU];
			img[i][j][BLU] = img[rows][cols][BLU];
			img[rows][cols][BLU] = tempt;

			cols--;
			rows--;
		}
	}
}



The reason you are getting an error is because the compiler needs to know the maximum size of every dimension except the first to do multi-dimensional indexing. If you know all the dimensions except the first one beforehand (they are constant), then it is quite simple:

#define MAX_COLOR 3
#define MAX_Y 600
#define MAX_Y 480

void swap (int img[][MAX_Y][MAX_COLOR] , int rows , int cols) {
   ...
}

int main() {
   int img[MAX_X, MAX_Y, MAX_COLOR];
   swap(img, MAX_X, MAX_Y);
}


This post has been edited by Karel-Lodewijk: 01 February 2012 - 04:29 PM

Was This Post Helpful? 0
  • +
  • -

#7 EveilAxe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 01-March 10

Re: problem on swapping Array in C

Posted 01 February 2012 - 04:36 PM

Same error even if I change the array to img[rows-1][cols-1][RED]; so as the others.
Was This Post Helpful? 0
  • +
  • -

#8 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 794
  • View blog
  • Posts: 2,880
  • Joined: 31-December 10

Re: problem on swapping Array in C

Posted 01 February 2012 - 04:53 PM

Could you post the bitmap.h file because we don't know what RED, GRN, and BLU are from what you've posted so far. For all we know, those could be the problem.
Was This Post Helpful? 0
  • +
  • -

#9 EveilAxe   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 21
  • Joined: 01-March 10

Re: problem on swapping Array in C

Posted 01 February 2012 - 05:07 PM

yea sure!! The following code is my bitmap.h file
#define RED 0  // image[r][c][RED] is red value of pixel (0 <= value <= 255)
#define GRN 1  // image[r][c][GRN] is green value of pixel (0 <= value <= 255)
#define BLU 2  // image[r][c][BLU] is blue value of pixel (0 <= value <= 255)


 /*
  * Function declarations:
  */
int ***ReadBitmapFile(char *filename, int *pROWS, int *pCOLS);
int    WriteBitmapFile(char *filename, int ***image, int ROWS, int COLS);
void   FreeBitmapImage(int ***image, int rows, int cols);
void BlackAndWhite (int ***image, int rows, int cols);
void swap (int ***image, int rows, int cols);
void reveals (int ***image, int rows, int cols);
void block (int ***img , int rows , int cols);


Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: problem on swapping Array in C

Posted 01 February 2012 - 07:17 PM

You could clean up your swap a little by actually using a swap function:
void swapValue(int *a, int *b ) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

void swap(int ***img , int rows , int cols) {
	int trows = rows;
	int tcols = cols;
	for(int i=0; i < trows /2 ; i++) {
		for(int j=0; j< tcols /2; j++) {
			swapValue(&img[i][j][RED], &img[rows][cols][RED]);
			swapValue(&img[i][j][GRN], &img[rows][cols][GRN]);
			swapValue(&img[i][j][BLU], &img[rows][cols][BLU]);
			cols--;
			rows--;
		}
	}
}



Of course, the real question is, what the hell are you doing? Let's write a test:
void swapTest(int rows, int cols) {
	int trows = rows;
	int tcols = cols;
	for(int i=0; i < trows /2 ; i++) {
		for(int j=0; j< tcols /2; j++) {
			printf("(%d,%d) <> (%d,%d)\n", i,j,rows,cols);
			cols--;
			rows--;
		}
	}
}

swapTest(9, 9);



Results:
(0,0) <> (9,9)
(0,1) <> (8,8)
(0,2) <> (7,7)
(0,3) <> (6,6)
(1,0) <> (5,5)
(1,1) <> (4,4)
(1,2) <> (3,3)
(1,3) <> (2,2)
(2,0) <> (1,1)
(2,1) <> (0,0)
(2,2) <> (-1,-1)
(2,3) <> (-2,-2)
(3,0) <> (-3,-3)
(3,1) <> (-4,-4)
(3,2) <> (-5,-5)
(3,3) <> (-6,-6)



When you start swapping to negative locations, you're done.

You seem to be using the third dimension as a record. Don't do that. Use struct. e.g.
typedef struct {
	unsigned char red, green, blue;
} Color;

typedef struct {
	int rows, cols;
	Color **data;
} Image;


Image ReadBitmapFile(const char *filename);
int    WriteBitmapFile(const char *filename, Image *);
void   FreeBitmapImage(Image *);
void BlackAndWhite (Image *);
void swap (Image *);



This has the bonus that a swap is as simple as:
Image *img;
Color tmp;
//...
tmp = img->data[i][j];
img->data[i][j] = img->data[row][col];
img->data[row][col] = tmp;



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1