Array function (image processing project) - c

I faced an issue of my assignment related the array function.
I want to make a border element with '0' at first three and last three rows and columns. Firstly , I able to generate the number of 256X256 size (array[256][256]).
Then, after the first array I generated that I need to do some condition .For example,
For element value <127, subtract 20 from the value.
For element value >127, add 20 to the value.
If the value of any element is <0 after the operation then assign the value 0 to it.
If the value of any element is >255 after the operation then assign the value 255 to
it.
The problem is when I generated again, the "0" border of element become different. How to i solve it to be like first array as seen like "0" border of element?
Below is my C++ code.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int inputimage[256][256], modifinputimage[256][256];
int i, j;
char ch;
for (i = 0; i < 256; i++)
{
if (i < 3)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 253)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 3 && i <253)
{
for (j = 0; j < 256; j++)
{
if ((i >= 3 && j < 3) || (i<253 && j >= 253))
{
printf("0\t");
}
if (j >= 3 && j < 253)
{
inputimage[i][j] = rand() % 256;
printf("%d\t", inputimage[i][j]);
}
}
}
}
printf("\nProceed to Contrast Adjustment ? (Press ENTER to continue)*** \n\n\n\n");
ch = getche();
for (i = 0; i < 256; i++)
{
if (i < 3)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 253)
{
for (j = 0; j < 256; j++)
{
if (j < 256)
{
printf("0\t");
}
}
}
else if (i >= 3 && i <253)
{
for (j = 0; j < 256; j++)
{
if ((i >= 3 && j < 3) || (i<253 && j >= 253))
{
printf("0\t");
}
if (j >= 3 && j < 253)
{
if (inputimage[i][j] < 127 && inputimage[i][j] >= 20)
{
modifinputimage[i][j] = inputimage[i][j] - 20;
printf("%1d\t", modifinputimage[i][j]);
}
if (inputimage[i][j] > 127 && inputimage[i][j] <= 235)
{
modifinputimage[i][j] = inputimage[i][j] + 20;
printf("%1d\t", modifinputimage[i][j]);
}
if (inputimage[i][j] <= 0)
{
modifinputimage[i][j] = inputimage[i][j];
printf("0\t");
}
if (inputimage[i][j] >= 255)
{
modifinputimage[i][j] = inputimage[i][j];
printf("255\t");
}
}
}
}
}
}

In your modification you need to use the approach of accessing the array by rows and columns by using two for loops at the start to have the position of both row and column. For Example
for (int i = 0; i < 256; i++){
for(int j = 0; j < 256; j++){
}
}
this way you are going through all the rows and columns and inside the above condition use
if(i <= 3 || i >= 253){
//here u can use the nested loop to print 0's for both first and last 3 rows
printf("0\t");
}
// similarly for columns you can use this
else if(j <= 3 || j >=253){
printf("0\t");
}
else if(i > 3 && i < 253 && j > 3 && j < 253){
//here your modification rules
}
hope this helps

Related

Print repeating diamond shapes with custom rows, columns and length

I am new to C, and I am trying to print diamond shapes according to the rows(2~10), columns(2~10) and the length(3, 5, 7, 9) of the diamond input from the user.
Using the code below I can print diamond and number of diamonds correctly, but I just can't get the correct distance between them.
void printDiamondWith(int diamondlength, int numberOfDiamonds) {
int i, j, k;
int star, space;
star = 1;
space = diamondlength;
for (i = 1; i < diamondlength * 2 - 1; i++) {
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space; j++) {
printf(" "); // Print the distance for the previous star
}
for (j = 1; j < star * 2; j++) {
printf("*");
}
for (j = 0; j < space; j++) {
printf(" "); // Print the distance for the next star
}
}
printf("\n");
// Check if length is equal 3, else length -1 to get the correct rows of second half of the diamond
if (diamondlength == 3) {
// Loops until the first half of the diamond is finished, then reverse the process to print the second half
if(i < (diamondlength - diamondlength / 3)) {
space--;
star++;
} else {
space++;
star--;
}
} else if (diamondlength >= 3) {
if (i < (diamondlength - 1 - diamondlength / 3)) {
space--;
star++;
} else {
space++;
star--;
}
}
}
}
Actual running result:
Expected result:
Your formulas for calculating the space is off. It works for me when I change this
space = diamondlength;
to this
space = diamondlength/2+1;
And this
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space; j++) {
to this:
for (k = 0; k < numberOfDiamonds; k++) {
for (j = 0; j < space-1; j++) {
In such situations I recommend hardcoding the variable for different parameters and write down what the variable has to be for what parameter so you can try to find a function that maps the parameter to the value. For instance I saw that as diamondlength increased, the space error also increased, so the relation between parameter and variable can't be one to one.

Why won't for loop terminate?

My function:
int checkSE(disk board[][SIZE], disk hypotheticalDisk)
{
int i;
int j;
int row;
int col;
int player;
int opponent;
int checkSEflag;
player = hypotheticalDisk.type;
(player == 0) ? (opponent = 1) : (opponent = 0);
row = hypotheticalDisk.pos.row;
col = hypotheticalDisk.pos.col;
checkSEflag = 0;
for (i = row + 2, j = col + 2; ((i < SIZE) && (j < SIZE) && (checkSEflag == 0)); i++, j++)
{
if (board[i][j].type == player)
{
for (--i, --j; board[i][j].type == opponent; i--, j--)
{
if (i == row && j == col)
{
checkSEflag = 1;
break;
}
}
}
printf("\n%d and %d and %d", i, j, checkSEflag);
}
return checkSEflag;
}
My output:
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
2 and 3 and 0
.
.
.
And it keeps on going...
I want both i and j to increase until they are equal to SIZE (SIZE predefined to be 8) or until checkSEflag is assigned to be equal to 1.
It looks like the values of i and j just aren't being changed...
I tried taking them out of the loop conditions and instead placed them
in the loop body, though that didn't change anything.
I doubt the post increment operators just decided to not work so I must be doing something wrong, any ideas of what that may be?
These two lines:
for(i = row+2, j = col+2; ((i < SIZE) && (j <SIZE) && (checkSEflag == 0)); i++, j++)
...
for(--i, --j; board[i][j].type == opponent; i--, j--)
so, you are both incrementing and decrementing (i,j); try sprinkling printfs around these and see if you are both incrementing and decrementing i,j on each iteration...

C - Populate matrix with some density

I've got this code to populate matrix with 0/1 values and RHO density. I need the same for values from 0 to 2. I mean, the percentage of zeros should be the same, but other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho;
}
}
The only thing I've been able to do is something inelegant like this. This leaves zero/non zero percentage inalterate and random modifies the 1 cells:
...
if(grid[cur][i][j] > 0) {
grid[cur][i][j] += rand()%2;
}
I think this code will create 0 with RHO density and other values in range 1-2.
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
grid[cur][i][j] = (((float)rand())/RAND_MAX) < rho ? 0 : rand() % 2 + 1;
}
}

How do I check to see if my array is in ascending order?

So I have an array board[][] whose values keep shuffling around. It is a square array with dimensions d*d. I want to check the array to see if all of its values are in ascending order.
for (int i = 0; i < d; i++)
{
for (int j = 0; i < d; i++)
{
if (board[i][j] == (d * i) + j + 1)
{
return true;
}
else
{
return false;
}
}
}
the problem is that as soon as the first element in the array board[0][0] = 1, it returns true, and ends my code. I don't know how to implement it so that it doesn't return true until all the elements in the array are in ascending order, from 1 to (d*d - 1).
Thank you!
If I understood your question correctly I think you want to do this:
int Previous = board[0][0];
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] < Previous)
{
return false;
}
Previous = board[i][j];
}
}
return true; // Only at the end do we know that all elements are in ascending order
Problems with what you have are:
You are returning on either condition instead of returning after all values have been checked
Your second for loop is wrong and references i, not j
Your comparison is comparing board values against the indexes, not other board values
This should work
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] != (d * i) + j + 1)
{
return false;
}
}
}
return true;
Try replacing return true with continue:
for (int i = 0; i < d; i++)
{
for (int j = 0; i < d; i++)
{
if (board[i][j] == (d * i) + j + 1)
{
continue;
}
else
{
return false;
}
}
}
You can also remove the continue clause completely if you reverse the condition:
if (board[i][j] != (d * i) + j + 1)
{
return false;
}
1) First what you mean by ascending order (vertically, horizontally or in diagonal ?)
2) Replace your if statement
if ((board[i][j] > board[i][j+1]) || (board[i][j] > board[i+1][j]))

C convert for loop into a while loop and recursion

I'm doing another exercise and I have to :
"Write a recursive function to print all solution to the eight queens chess problem, return the number of solutions and the function prototype must be : int function(void)
To work around the no argument rule I used static variables.
I've done it (with google's help), and it works, but they don't allow to use for loops and for some reason I can't manage to convert the last two for loops to while loops.
It' driving me crazy, it should be easy! I think it's the recursion that mess it up...
Here's the working function :
int function()
{
static int count = 0;
static int col = 0;
const int n = 8;
static int hist[8] = {10, 10, 10, 10, 10, 10, 10, 10};
int i1 = 0;
if (col == n) {
count++;
while (i1++ < n)
{
putchar('0' + hist[i1-1] + 1);
}
putchar('\n');
}
for (int i = 0; i < n; i++) {
int j = 0;
for (j = 0; j < col && !(hist[j] == i || (hist[j] - i) == col - j || -(hist[j] - i) == col - j); j++);
if (j < col) {
continue;
}
hist[col] = i;
col++;
function();
col--;
}
return count;
}
And I tried to convert the two last for loops to while loops like this :
int i = 0;
while (i < n)
{
int j = 0;
while (j < col && !(hist[j] == i || (hist[j] - i) == col - j || -(hist[j] - i) == col - j))
{
j++;
}
if (j < col) {
continue;
}
hist[col] = i;
col++;
function();
col--;
i++;
}
But it doesn't work, is there more the for loops than it seems ? I'm new to recursion, I thought I got it but it seems I was wrong...
I ran the code and found the problem. It is with the line
if (j < col) {
continue;
}
since this isn't a continue statement that goes to a for loop, you have to increment i in this condition as well.
if (j < col) {
i++; // add this line
continue;
}
You can change the first loop to
while(i++<n)
and it works fine.

Resources