C - Recursion instead of loops - c

I made a program that evaluates expressions, for example
Enter text: 97+74/51-98-11+68-34-2-22+73/40+81/15+100
output :
105.36
If any wrong data is given a user gets a response Incorrect input
I am wondering if I could replace all my for loops to recursion, but I don't know how could I achieve that. Could anybody help ?
Any assistance would be much appreciated.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<math.h>
int my_atoi(const char* tab, int from, int to)
{
int score = 0;
int minus = 0;
for (int i = from; i <= to; i++)
{
if (*(tab + i) == 10)
break;
if (*(tab + i) == 32)
break;
if (*(tab + i) == '-' && from == i)
{
minus = 1;
continue;
}
if (*(tab + i) == '+' && i == from)
{
continue;
}
if (*(tab + i) > '9' || *(tab + i) < '0')
break;
else if (*(tab + i) >= '0' && *(tab + i) <= '9')
{
score *= 10;
score += (*(tab + i) - '0');
}
}
if (minus == 1)
score *= -1;
return score;
}
int validate_expression(const char* expr)
{
if (!expr)return -1;
int lng = strlen(expr) - 1;
if (*(expr) > '9' || *(expr) < '0' || * (expr + lng) > '9' || *(expr + lng) < '0')return 0;
int flaga = 0; // flag
for (int i = 1; i < lng; i++) {
if (*(expr + i) <= '9' && *(expr + i) >= '0')
{
flaga = 0;
}
else if (*(expr + i) == '/' || *(expr + i) == '*' || *(expr + i) == '-' || *(expr + i) == '+')
{
if (flaga == 1)
return 0;
flaga = 1;
}
else
return 0;
}
return 1;
}
int calculate(const char* expr, float* result)
{
if (!expr || !result || !validate_expression(expr)) return 0;
if (strpbrk(expr, "i") != NULL) return 0;
int index = 0;
float score = 0;
char sign_memory = 'A';
int flaga = 0;
unsigned int i = 1;
for (; i <= strlen(expr); i++) {
if (*(expr + i) == '-' || *(expr + i) == '+' || *(expr + i) == '/' || *(expr + i) == '*' || *(expr + i) == '\0') {
score += (int)my_atoi(expr, index, i - 1);
index = i;
break;
}
}
for (; i <= strlen(expr); i++) {
if (*(expr + i) == '-' || *(expr + i) == '+' || *(expr + i) == '/' || *(expr + i) == '*' || i == strlen(expr)) {
flaga = 1;
if (sign_memory == '+')
score += (int)my_atoi(expr, index, i - 1);
else if (sign_memory == '-')
score -= (int)my_atoi(expr, index, i - 1);
else if (sign_memory == '/')
{
if (my_atoi(expr, index, i - 1) == 0)return 0;
score /= (int)my_atoi(expr, index, i - 1);
}
else if (sign_memory == '*')
score *= (int)my_atoi(expr, index, i - 1);
sign_memory = *(expr + i);
}
else if (*(expr + i) <= '9' && *(expr + i) >= '0' && flaga == 1) {
flaga = 0;
index = i;
}
}
*result = score;
return 1;
}
int main()
{
char tab[201];
float result, * pointer = &result;
printf("Enter text: ");
fgets(tab, sizeof(tab), stdin);
*(tab + strlen(tab) - 1) = '\0';
if (!validate_expression(tab))
{
printf("Incorrect input");
return 1;
}
else
{
if (calculate(tab, pointer))
printf("%.2f", *pointer);
else {
printf("Incorrect input");
return 1;
}
}
return 0;
}

I would make a function, which does the following:
Check if there is just one or more operators ("+", "-", "*", "/")
In case there is only one, perform the calculation.
In case there are two, perform a tail recursion, as proposed by Jonathan, something like:
calculate("97+74/51-98-11+68-34-2-22+73/40+81/15+100")
= calculate("97+74/51-98-11+68-34-2-22+73/40+81/15") + 100
= (calculate("97+74/51-98-11+68-34-2-22+73/40+81") / 15) + 100
= ((calculate("97+74/51-98-11+68-34-2-22+73/40") + 81) / 15) + 100
= (((calculate("97+74/51-98-11+68-34-2-22+73") / 40) + 81) / 15) + 100
= ((((calculate("97+74/51-98-11+68-34-2-22") + 73) / 40) + 81) / 15) + 100
= (((((calculate("97+74/51-98-11+68-34-2") - 22) + 73) / 40) + 81) / 15) + 100
= ((((((calculate("97+74/51-98-11+68-34") - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((calculate("97+74/51-98-11+68") - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((calculate("97+74/51-98-11") + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((((calculate("97+74/51-98") - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((((calculate("97+74/51") - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= (((((((((((calculate("97+74") / 51) - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100
= ((((((((((((97 + 74) / 51) - 98) - 11) + 68) - 34) - 2) - 22) + 73) / 40) + 81) / 15) + 100

Related

passing RGBTRIPLE to parameter of incompatible type error

I am working on a project for CS50 which requires me to make a code that turns image to be edges or blur or grey or reflected image.
I did my task successfully but I am facing an error in the edge function that I couldn't solve so can you help me solving it ?
The project composed of 4 files: bmp.h, filter.c, helpers.h and helpers.c. I am required to edit the program helpers.c so I make 6 functions to get the summation of each colour values multiplied by some factors for 9 pixels which make a box inside its center the pixel which its values for 3 colors will be edited according to Sobel filter algorithm, so here are the code inside the 4 files but I don't know why filter.c can't compile smoothly.
bmp.h file code:
// BMP-related data types based on Microsoft's own
#include <stdint.h>
typedef uint8_t BYTE;
typedef uint32_t DWORD;
typedef int32_t LONG;
typedef uint16_t WORD;
/**
* BITMAPFILEHEADER
*
* The BITMAPFILEHEADER structure contains information about the type, size,
* and layout of a file that contains a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183374(VS.85).aspx.
*/
typedef struct
{
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} __attribute__((__packed__))
BITMAPFILEHEADER;
/**
* BITMAPINFOHEADER
*
* The BITMAPINFOHEADER structure contains information about the
* dimensions and color format of a DIB [device-independent bitmap].
*
* Adapted from http://msdn.microsoft.com/en-us/library/dd183376(VS.85).aspx.
*/
typedef struct
{
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} __attribute__((__packed__))
BITMAPINFOHEADER;
/**
* RGBTRIPLE
*
* This structure describes a color consisting of relative intensities of
* red, green, and blue.
*
* Adapted from http://msdn.microsoft.com/en-us/library/aa922590.aspx.
*/
typedef struct
{
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} __attribute__((__packed__))
RGBTRIPLE;
filter.c file code:
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <cs50.h>
#include "helpers.h"
/*
int Gx_red (int index_i, int index_j, int width, int height, int red_11, int red_12, int red_13, int red_21, int red_22, int red_23,
int red_31, int red_32, int red_33, RGBTRIPLE image[height][width]);
int Gx_blue (int index_i, int index_j, int width, int height, int blue_11, int blue_12, int blue_13, int blue_21, int blue_22,
int blue_23, int blue_31, int blue_32, int blue_33, RGBTRIPLE image[height][width]);
int Gx_green (int index_i, int index_j, int width, int height, int green_11, int green_12, int green_13, int green_21, int green_22,
int green_23, int green_31, int green_32, int green_33, RGBTRIPLE image[height][width]);
int Gy_red (int index_i, int index_j, int width, int height, int red_11, int red_12, int red_13, int red_21, int red_22,
int red_23, int red_31, int red_32, int red_33, RGBTRIPLE image[height][width]);
int Gy_blue (int index_i, int index_j, int width, int height, int blue_11, int blue_12, int blue_13, int blue_21,
int blue_22, int blue_23, int blue_31, int blue_32, int blue_33, RGBTRIPLE image[height][width]);
int Gy_green (int index_i, int index_j, int width, int height, int green_11, int green_12, int green_13, int green_21,
int green_22, int green_23, int green_31, int green_32, int green_33, RGBTRIPLE image[height][width]);
*/
int main(int argc, char *argv[])
{
// Define allowable filters
char *filters = "begr";
// Get filter flag and check validity
char filter = getopt(argc, argv, filters);
printf("optind is %d, filter is %c & %i, argc is %d and argv is %s\n", optind, filter, getopt(argc, argv, filters), argc, argv[optind - 1]);
if (filter == '?')
{
printf("Invalid filter.\n");
return 1;
}
// Ensure only one filter
if (getopt(argc, argv, filters) != -1)
{
printf("Only one filter allowed.\n");
return 2;
}
// Ensure proper usage
if (argc != optind + 2)
{
printf("Usage: ./filter [flag] infile outfile\n");
return 3;
}
// Remember filenames
char *infile = argv[optind];
char *outfile = argv[optind + 1];
// Open input file
FILE *inptr = fopen(infile, "r");
if (inptr == NULL)
{
printf("Could not open %s.\n", infile);
return 4;
}
// Open output file
FILE *outptr = fopen(outfile, "w");
if (outptr == NULL)
{
fclose(inptr);
printf("Could not create %s.\n", outfile);
return 5;
}
// Read infile's BITMAPFILEHEADER
BITMAPFILEHEADER bf;
fread(&bf, sizeof(BITMAPFILEHEADER), 1, inptr);
// Read infile's BITMAPINFOHEADER
BITMAPINFOHEADER bi;
fread(&bi, sizeof(BITMAPINFOHEADER), 1, inptr);
// Ensure infile is (likely) a 24-bit uncompressed BMP 4.0
if (bf.bfType != 0x4d42 || bf.bfOffBits != 54 || bi.biSize != 40 ||
bi.biBitCount != 24 || bi.biCompression != 0)
{
fclose(outptr);
fclose(inptr);
printf("Unsupported file format.\n");
return 6;
}
// Get image's dimensions
int height = abs(bi.biHeight);
int width = bi.biWidth;
// Allocate memory for image
RGBTRIPLE(*image)[width] = calloc(height, width * sizeof(RGBTRIPLE));
if (image == NULL)
{
printf("Not enough memory to store image.\n");
fclose(outptr);
fclose(inptr);
return 7;
}
// Determine padding for scanlines
int padding = (4 - (width * sizeof(RGBTRIPLE)) % 4) % 4;
// Iterate over infile's scanlines
for (int i = 0; i < height; i++)
{
// Read row into pixel array
fread(image[i], sizeof(RGBTRIPLE), width, inptr);
// Skip over padding
fseek(inptr, padding, SEEK_CUR);
}
// Filter image
switch (filter)
{
// Blur
case 'b':
blur(height, width, image);
break;
// Edges
case 'e':
edges(height, width, image);
break;
// Grayscale
case 'g':
grayscale(height, width, image);
break;
// Reflect
case 'r':
reflect(height, width, image);
break;
}
// Write outfile's BITMAPFILEHEADER
fwrite(&bf, sizeof(BITMAPFILEHEADER), 1, outptr);
// Write outfile's BITMAPINFOHEADER
fwrite(&bi, sizeof(BITMAPINFOHEADER), 1, outptr);
// Write new pixels to outfile
for (int i = 0; i < height; i++)
{
// Write row to outfile
fwrite(image[i], sizeof(RGBTRIPLE), width, outptr);
// Write padding at end of row
for (int k = 0; k < padding; k++)
{
fputc(0x00, outptr);
}
}
// Free memory for image
free(image);
// Close files
fclose(inptr);
fclose(outptr);
return 0;
}
helpers.c file code:
#include <stdio.h>
#include <math.h>
#include "helpers.h"
//#include "bmp.h"
//variables for
int Gx_sum_red, Gx_sum_blue, Gx_sum_green, Gy_sum_red, Gy_sum_blue, Gy_sum_green;
int red_avg, blue_avg, green_avg;
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
image[i][j].rgbtBlue = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtGreen = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
image[i][j].rgbtRed = (image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width / 2; j++)
{
temp.rgbtBlue = image[i][j].rgbtBlue;
temp.rgbtGreen = image[i][j].rgbtGreen;
temp.rgbtRed = image[i][j].rgbtRed;
image[i][j].rgbtBlue = image[i][width - j].rgbtBlue;
image[i][j].rgbtGreen = image[i][width - j].rgbtGreen;
image[i][j].rgbtRed = image[i][width - j].rgbtRed;
image[i][width - j].rgbtBlue = temp.rgbtBlue;
image[i][width - j].rgbtGreen = temp.rgbtGreen;
image[i][width - j].rgbtRed = temp.rgbtRed;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
return;
}
//calculate Gx for red
int Gx_red (int index_i, int index_j, int width, int height, int red_11, int red_12, int red_13, int red_21, int red_22, int red_23,
int red_31, int red_32, int red_33, RGBTRIPLE image[height][width])
{
int fGx_sum_red = 0;
if (index_i == 0 && index_j == 0)
{
fGx_sum_red = (red_22 * 0) + (red_23 * 2) + (red_32 * 0) + (red_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGx_sum_red = (red_21 * -2) + (red_22 * 0) + (red_31 * -1) + (red_32 * 0);
}
else if (index_i == height - 1 && index_j == 0)
{
fGx_sum_red = (red_12 * 0) + (red_13 * 1) + (red_22 * 0) + (red_23 * 2);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGx_sum_red = (red_11 * -1) + (red_12 * 0) + (red_21 * -2) + (red_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_red = (red_12 * 0) + (red_13 * 1) + (red_22 * 0) + (red_23 * 2) + (red_32 * 0) + (red_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_red = (red_11 * -1) + (red_12 * 0) + (red_13 * 1) + (red_21 * -2) + (red_22 * 0) + (red_23 * 2);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_red = (red_12 * 0) + (red_13 * 1) + (red_22 * 0) + (red_23 * 2) + (red_32 * 0) + (red_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_red = (red_11 * -1) + (red_12 * 0) + (red_21 * -2) + (red_22 * 0) + (red_31 * -1) + (red_32 * 0);
}
else
{
fGx_sum_red = (red_11 * -1) + (red_12 * 0) + (red_13 * 1) + (red_21 * -2) + (red_22 * 0) + (red_23 * 2) + (red_31 * -1) + (red_32 * 0) +
(red_33 * 1);
}
return fGx_sum_red;
}
//calculate Gx for blue
int Gx_blue (int index_i, int index_j, int width, int height, int blue_11, int blue_12, int blue_13, int blue_21, int blue_22,
int blue_23, int blue_31, int blue_32, int blue_33, RGBTRIPLE image[height][width])
{
int fGx_sum_blue = 0;
if (index_i == 0 && index_j == 0)
{
fGx_sum_blue = (blue_22 * 0) + (blue_23 * 2) + (blue_32 * 0) + (blue_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGx_sum_blue = (blue_21 * -2) + (blue_22 * 0) + (blue_31 * -1) + (blue_32 * 0);
}
else if (index_i == height - 1 && index_j == 0)
{
fGx_sum_blue = (blue_12 * 0) + (blue_13 * 1) + (blue_22 * 0) + (blue_23 * 2);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGx_sum_blue = (blue_11 * -1) + (blue_12 * 0) + (blue_21 * -2) + (blue_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_blue = (blue_12 * 0) + (blue_13 * 1) + (blue_22 * 0) + (blue_23 * 2) + (blue_32 * 0) + (blue_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_blue = (blue_11 * -1) + (blue_12 * 0) + (blue_13 * 1) + (blue_21 * -2) + (blue_22 * 0) + (blue_23 * 2);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_blue = (blue_12 * 0) + (blue_13 * 1) + (blue_22 * 0) + (blue_23 * 2) + (blue_32 * 0) + (blue_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_blue = (blue_11 * -1) + (blue_12 * 0) + (blue_21 * -2) + (blue_22 * 0) + (blue_31 * -1) + (blue_32 * 0);
}
else
{
fGx_sum_blue = (blue_11 * -1) + (blue_12 * 0) + (blue_13 * 1) + (blue_21 * -2) + (blue_22 * 0) + (blue_23 * 2) + (blue_31 * -1) +
(blue_32 * 0) + (blue_33 * 1);
}
printf("fGx_sum_blue is %i\n", fGx_sum_blue);
return fGx_sum_blue;
}
//calculate Gx for green
int Gx_green (int index_i, int index_j, int width, int height, int green_11, int green_12, int green_13, int green_21, int green_22,
int green_23, int green_31, int green_32, int green_33, RGBTRIPLE image[height][width])
{
int fGx_sum_green = 0;
if (index_i == 0 && index_j == 0)
{
fGx_sum_green = (green_22 * 0) + (green_23 * 2) + (green_32 * 0) + (green_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGx_sum_green = (green_21 * -2) + (green_22 * 0) + (green_31 * -1) + (green_32 * 0);
}
else if (index_i == height - 1 && index_j == 0)
{
fGx_sum_green = (green_12 * 0) + (green_13 * 1) + (green_22 * 0) + (green_23 * 2);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGx_sum_green = (green_11 * -1) + (green_12 * 0) + (green_21 * -2) + (green_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_green = (green_12 * 0) + (green_13 * 1) + (green_22 * 0) + (green_23 * 2) + (green_32 * 0) + (green_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGx_sum_green = (green_11 * -1) + (green_12 * 0) + (green_13 * 1) + (green_21 * -2) + (green_22 * 0) + (green_23 * 2);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_green = (green_12 * 0) + (green_13 * 1) + (green_22 * 0) + (green_23 * 2) + (green_32 * 0) + (green_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGx_sum_green = (green_11 * -1) + (green_12 * 0) + (green_21 * -2) + (green_22 * 0) + (green_31 * -1) + (green_32 * 0);
}
else
{
fGx_sum_green = (green_11 * -1) + (green_12 * 0) + (green_13 * 1) + (green_21 * -2) + (green_22 * 0) + (green_23 * 2) + (green_31 * -1) +
(green_32 * 0) + (green_33 * 1);
}
return fGx_sum_green;
}
//calculate Gy for red
int Gy_red (int index_i, int index_j, int width, int height, int red_11, int red_12, int red_13, int red_21, int red_22,
int red_23, int red_31, int red_32, int red_33, RGBTRIPLE image[height][width])
{
int fGy_sum_red = 0;
if (index_i == 0 && index_j == 0)
{
fGy_sum_red = (red_22 * 0) + (red_23 * 0) + (red_32 * 2) + (red_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGy_sum_red = (red_21 * 0) + (red_22 * 0) + (red_31 * 1) + (red_32 * 2);
}
else if (index_i == height - 1 && index_j == 0)
{
fGy_sum_red = (red_12 * -2) + (red_13 * -1) + (red_22 * 0) + (red_23 * 0);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGy_sum_red = (red_11 * -1) + (red_12 * -2) + (red_21 * 0) + (red_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_red = (red_12 * -2) + (red_13 * -1) + (red_22 * 0) + (red_23 * 0) + (red_32 * 2) + (red_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_red = (red_11 * -1) + (red_12 * -2) + (red_13 * -1) + (red_21 * 0) + (red_22 * 0) + (red_23 * 0);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_red = (red_12 * -2) + (red_13 * -1) + (red_22 * 0) + (red_23 * 0) + (red_32 * 2) + (red_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_red = (red_11 * -1) + (red_12 * -2) + (red_21 * 0) + (red_22 * 0) + (red_31 * 1) + (red_32 * 2);
}
else
{
fGy_sum_red = (red_11 * -1) + (red_12 * -2) + (red_13 * -1) + (red_21 * 0) + (red_22 * 0) + (red_23 * 0) + (red_31 * 1) + (red_32 * 2) +
(red_33 * 1);
}
return fGy_sum_red;
}
//calculate Gy for blue
int Gy_blue (int index_i, int index_j, int width, int height, int blue_11, int blue_12, int blue_13, int blue_21,
int blue_22, int blue_23, int blue_31, int blue_32, int blue_33, RGBTRIPLE image[height][width])
{
int fGy_sum_blue = 0;
if (index_i == 0 && index_j == 0)
{
fGy_sum_blue = (blue_22 * 0) + (blue_23 * 0) + (blue_32 * 2) + (blue_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGy_sum_blue = (blue_21 * 0) + (blue_22 * 0) + (blue_31 * 1) + (blue_32 * 2);
}
else if (index_i == height - 1 && index_j == 0)
{
fGy_sum_blue = (blue_12 * -2) + (blue_13 * -1) + (blue_22 * 0) + (blue_23 * 0);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGy_sum_blue = (blue_11 * -1) + (blue_12 * -2) + (blue_21 * 0) + (blue_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_blue = (blue_12 * -2) + (blue_13 * -1) + (blue_22 * 0) + (blue_23 * 0) + (blue_32 * 2) + (blue_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_blue = (blue_11 * -1) + (blue_12 * -2) + (blue_13 * -1) + (blue_21 * 0) + (blue_22 * 0) + (blue_23 * 0);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_blue = (blue_12 * -2) + (blue_13 * -1) + (blue_22 * 0) + (blue_23 * 0) + (blue_32 * 2) + (blue_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_blue = (blue_11 * -1) + (blue_12 * -2) + (blue_21 * 0) + (blue_22 * 0) + (blue_31 * 1) + (blue_32 * 2);
}
else
{
fGy_sum_blue = (blue_11 * -1) + (blue_12 * -2) + (blue_13 * -1) + (blue_21 * 0) + (blue_22 * 0) + (blue_23 * 0) + (blue_31 * 1) +
(blue_32 * 2) + (blue_33 * 1);
}
return fGy_sum_blue;
}
//calculate Gy for green
int Gy_green (int index_i, int index_j, int width, int height, int green_11, int green_12, int green_13, int green_21,
int green_22, int green_23, int green_31, int green_32, int green_33, RGBTRIPLE image[height][width])
{
int fGy_sum_green = 0;
if (index_i == 0 && index_j == 0)
{
fGy_sum_green = (green_22 * 0) + (green_23 * 0) + (green_32 * 2) + (green_33 * 1);
}
else if (index_i == 0 && index_j == width - 1)
{
fGy_sum_green = (green_21 * 0) + (green_22 * 0) + (green_31 * 1) + (green_32 * 2);
}
else if (index_i == height - 1 && index_j == 0)
{
fGy_sum_green = (green_12 * -2) + (green_13 * -1) + (green_22 * 0) + (green_23 * 0);
}
else if (index_i == height - 1 && index_j == width - 1)
{
fGy_sum_green = (green_11 * -1) + (green_12 * -2) + (green_21 * 0) + (green_22 * 0);
}
else if (index_i == 0 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_green = (green_12 * -2) + (green_13 * -1) + (green_22 * 0) + (green_23 * 0) + (green_32 * 2) + (green_33 * 1);
}
else if (index_i == height - 1 && (index_j != 0 || index_j != width - 1))
{
fGy_sum_green = (green_11 * -1) + (green_12 * -2) + (green_13 * -1) + (green_21 * 0) + (green_22 * 0) + (green_23 * 0);
}
else if (index_j == 0 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_green = (green_12 * -2) + (green_13 * -1) + (green_22 * 0) + (green_23 * 0) + (green_32 * 2) + (green_33 * 1);
}
else if (index_j == width - 1 && (index_i != 0 || index_i != height - 1))
{
fGy_sum_green = (green_11 * -1) + (green_12 * -2) + (green_21 * 0) + (green_22 * 0) + (green_31 * 1) + (green_32 * 2);
}
else
{
fGy_sum_green = (green_11 * -1) + (green_12 * -2) + (green_13 * -1) + (green_21 * 0) + (green_22 * 0) + (green_23 * 0) + (green_31 * 1) +
(green_32 * 2) + (green_33 * 1);
}
return fGy_sum_green;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
Gx_sum_red = Gx_red (i, j, width, height, image[i - 1][j - 1].rgbtRed, image[i - 1][j].rgbtRed, image[i - 1][j + 1].rgbtRed,
image[i][j - 1].rgbtRed, image[i][j].rgbtRed, image[i][j + 1].rgbtRed, image[i + 1][j - 1].rgbtRed,
image[i + 1][j].rgbtRed, image[i + 1][j + 1].rgbtRed, image[height][width]);
Gx_sum_blue = Gx_blue (i, j, width, height, image[i - 1][j - 1].rgbtBlue, image[i - 1][j].rgbtBlue, image[i - 1][j + 1].rgbtBlue,
image[i][j - 1].rgbtBlue, image[i][j].rgbtBlue, image[i][j + 1].rgbtBlue, image[i + 1][j - 1].rgbtBlue,
image[i + 1][j].rgbtBlue, image[i + 1][j + 1].rgbtBlue, image[height][width]);
Gx_sum_green = Gx_green (i, j, width, height, image[i - 1][j - 1].rgbtGreen, image[i - 1][j].rgbtGreen, image[i - 1][j + 1].rgbtGreen,
image[i][j - 1].rgbtGreen, image[i][j].rgbtGreen, image[i][j + 1].rgbtGreen, image[i + 1][j - 1].rgbtGreen,
image[i + 1][j].rgbtGreen, image[i + 1][j + 1].rgbtGreen, image[height][width]);
Gy_sum_red = Gy_red (i, j, width, height, image[i - 1][j - 1].rgbtRed, image[i - 1][j].rgbtRed, image[i - 1][j + 1].rgbtRed,
image[i][j - 1].rgbtRed, image[i][j].rgbtRed, image[i][j + 1].rgbtRed, image[i + 1][j - 1].rgbtRed,
image[i + 1][j].rgbtRed, image[i + 1][j + 1].rgbtRed, image[height][width]);
Gy_sum_blue = Gy_blue (i, j, width, height, image[i - 1][j - 1].rgbtBlue, image[i - 1][j].rgbtBlue, image[i - 1][j + 1].rgbtBlue,
image[i][j - 1].rgbtBlue, image[i][j].rgbtBlue, image[i][j + 1].rgbtBlue, image[i + 1][j - 1].rgbtBlue,
image[i + 1][j].rgbtBlue, image[i + 1][j + 1].rgbtBlue, image[height][width]);
Gy_sum_green = Gy_green (i, j, width, height, image[i - 1][j - 1].rgbtGreen, image[i - 1][j].rgbtGreen, image[i - 1][j + 1].rgbtGreen,
image[i][j - 1].rgbtGreen, image[i][j].rgbtGreen, image[i][j + 1].rgbtGreen, image[i + 1][j - 1].rgbtGreen,
image[i + 1][j].rgbtGreen, image[i + 1][j + 1].rgbtGreen, image[height][width]);
//calculating sum of Gx and Gy for each color
red_avg = sqrt((Gx_sum_red ^ 2) + (Gy_sum_red ^ 2));
blue_avg = sqrt((Gx_sum_blue ^ 2) + (Gy_sum_blue ^ 2));
green_avg = sqrt((Gx_sum_green ^ 2) + (Gy_sum_green ^ 2));
printf("red_avg is %i, blue_avg is %i and green_avg is %i\n", red_avg, blue_avg, green_avg);
//updating colors values
image[i][j].rgbtRed = red_avg;
image[i][j].rgbtBlue = blue_avg;
image[i][j].rgbtGreen = green_avg;
}
}
return;
}
helpers.h file code:
#include "bmp.h"
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width]);
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width]);
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width]);
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width]);
so please tell me what is the error and how I can solve such an error?

How to fix filling the struct with these data in c

I have struct input parameter and array of it called input_arr. I want to fill the array with the text as it gives the wrong value for the id and it work correctly with the name and nothing appear in visible.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct input_parameter {
int id;
char name[30];
int position;
char visible[5];
char required[5];
char parameter_type;
char client_id[5];
int min_length;
int max_length;
char confirm_required[5];
};
struct input_parameter input_arr[10];
char text[2*1024]="{\"success\": true,\"language\":
\"en\",\"action\":
\"GetServiceInputParameterList\",\"version\": 1,\"data\": {
\"input_parameter_list\": [{\"id\": 1489,\"service_id\":
12102,\"name\": \"Customer Number\",\"position\":
1,\"visible\":
true,\"required\": true,\"parameter_type\":
\"N\",\"client_id\":
true,\"min_length\": 11, \"max_length\":
11,\"confirm_required\":
false } ] }}";
Fill an array of structs with the text:
int main() {
int i = 0;
int Wstart = 0;
int Wend = 0;
char name[19] = {0x20};
char name1[19] = {0x20};
int menunum = 0;
int len = strlen(text);
while (1) // while ALL
{
if (i >= len) {
break;
}
if (text[i] == 'i' && text[i + 1] == 'd') {
while (1) { // while id
if (text[i] == ':') {
Wstart = i + 1;
Wend = 0;
i++;
} else if (text[i] == ',' || text[i] == '}') {
Wend = i;
strncpy(name, text + Wstart, Wend - Wstart);
input_arr[menunum].id = atoi(name);
memset(name, 0, sizeof(name));
i++;
break;
} else {
i = i + 1;
}
} // while id
} else if (text[i] == 'n' && text[i + 1] == 'a' && text[i + 2] == 'm' &&
text[i + 3] == 'e') {
while (1) { // while name
if (text[i] == ':') {
Wstart = i + 3;
Wend = 0;
i++;
} else if (text[i] == ',' || text[i] == '}') {
Wend = i - 1;
strncpy(name, text + Wstart, Wend - Wstart);
// name[Wend-Wstart] = '\0';
// memset(name1, 0, sizeof(name1));
if ((name[1] >= 'a' && name[1] <= 'z') ||
(name[1] >= 'A' && name[1] <= 'Z')) {
// printf("%c is an alphabet.",c);
strcpy(name1, name);
} else {
int vc = 0;
int ia = strlen(name) - 1;
for (ia = strlen(name) - 1; ia >= 0; ia--) {
name1[vc] = name[ia];
vc++;
}
}
strcpy(input_arr[menunum].name, name1);
menunum++;
memset(name, 0, sizeof(name));
i++;
break;
} else {
i = i + 1;
}
} // while name
} else if (text[i] == 'v' && text[i + 1] == 'i' && text[i + 2] == 's' &&
text[i + 3] == 'i' && text[i + 4] == 'b' &&
text[i + 5] == 'l' && text[i + 6] == 'e') {
while (1) { // while visible
if (text[i] == ':') {
Wstart = i + 3;
Wend = 0;
i++;
} else if (text[i] == ',' || text[i] == '}') {
Wend = i - 1;
strncpy(name, text + Wstart, Wend - Wstart);
// name[Wend-Wstart] = '\0';
memset(name1, 0, sizeof(name1));
if ((name[1] >= 'a' && name[1] <= 'z') ||
(name[1] >= 'A' && name[1] <= 'Z')) {
// printf("%c is an alphabet.",c);
strcpy(name1, name);
} else {
int vc = 0;
int ia = strlen(name) - 1;
for (ia = strlen(name) - 1; ia >= 0; ia--) {
name1[vc] = name[ia];
vc++;
}
}
strcpy(input_arr[menunum].visible, name1);
menunum++;
// memset(name, 0, sizeof(name));
i++;
break;
} else {
i = i + 1;
}
} // while visible
} else {
i++;
}
}
printf("id:%d \n name: %s \n visible: %s
\n",&input_arr[0].id,&input_arr[0].name,&input_arr[0].visible);
return 0;
}
Well you are printing address of id instead of its value using %d format specifier.
printf("id:%d\nname: %s\n visible: %d\n",&input_arr[0].id,&input_arr[0].name,&input_arr[0].visible);
should be
printf("id:%d \n name: %s \n visible: %d\n",input_arr[0].id,input_arr[0].name,input_arr[0].visible);

SetConsoleCursorPosition : Getting black rows in console upon use

Following my previous question about making a snake program more fluid, I went and tried ANSI escape sequences and console functions to put back the text cursor on the top left corner.
I want to get the cursor on the top left corner of the screen, but while it does so I get black stripes across the screen every other line.
I am working in a windows environment making this program for the windows console.
Here is the painting function :
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
I tried putting the escape code and console function in the main right before calling the paint function but I got the same results.
here is the whole program if someone wants to test :
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <Windows.h>
void paint(int tab[28][120], int ligneMax, int colonneMax, HANDLE hconsole)
{
//system("cls");
//printf("\033[1;1H");
COORD destCoord;
destCoord.X = 0;
destCoord.Y = 0;
SetConsoleCursorPosition(hconsole, destCoord);
for (int i = 0; i < ligneMax; i++)
{
for (int j = 0; j < colonneMax; j++)
{
printf("%c", tab[i][j]);
}
printf("\n");
}
}
void create(int tab[28][120], int Nbligne, int Nbcolonne,
int randligne, int randcols)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
tab[i][j] = ' ';
if (i == 0 || i == Nbligne - 1)
tab[i][j] = 205;
if (j == 0 || j == Nbcolonne - 1)
tab[i][j] = 186;
if (i == 0 && j == 0)
tab[i][j] = 201;
if (i == 0 && j == Nbcolonne - 1)
tab[i][j] = 187;
if (i == Nbligne - 1 && j == 0)
tab[i][j] = 200;
if (i == Nbligne - 1 && j == Nbcolonne - 1)
tab[i][j] = 188;
if (i == 14 && j == 60)
tab[i][j] = 219;
if (i == 14 && j == 59)
tab[i][j] = 79;
if (i == 14 && j == 58)
tab[i][j] = 35;
if (i == randligne && j == randcols)
tab[i][j] = 176;
}
}
}
void destroyTail(int tab[28][120], int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 35)
{
tab[i][j] = ' ';
if (tab[i][j + 1] == 79)
tab[i][j + 1] = 35;
else if (tab[i][j - 1] == 79)
tab[i][j - 1] = 35;
else if (tab[i + 1][j] == 79)
tab[i + 1][j] = 35;
else if (tab[i - 1][j] == 79)
tab[i - 1][j] = 35;
goto stop;
}
}
}
stop: NULL;
}
void translate(int tab[28][120], char direction, int Nbligne, int Nbcolonne)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if (tab[i][j] == 219)
{
if (direction == 'R')
{
tab[i][j] = 79;
tab[i][j + 1] = 219;
}
if (direction == 'D')
{
tab[i][j] = 79;
tab[i + 1][j] = 219;
}
if (direction == 'L')
{
tab[i][j] = 79;
tab[i][j - 1] = 219;
}
if (direction == 'U')
{
tab[i][j] = 79;
tab[i - 1][j] = 219;
}
goto stop;
}
}
}
stop: NULL;
}
int checkExpand(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && tab[i][j + 1] == 176) ||
(direction == 'L' && tab[i][j] == 219 && tab[i][j - 1] == 176) ||
(direction == 'U' && tab[i][j] == 219 && tab[i - 1][j] == 176) ||
(direction == 'D' && tab[i][j] == 219 && tab[i + 1][j] == 176))
return 1;
}
}
return 0;
}
int checkDeath(int tab[28][120], int Nbligne, int Nbcolonne, char direction)
{
for (int i = 0; i < Nbligne; i++)
{
for (int j = 0; j < Nbcolonne; j++)
{
if ((direction == 'R' && tab[i][j] == 219 && (tab[i][j + 1] == 186 || tab[i][j + 1] == 79)) ||
(direction == 'L' && tab[i][j] == 219 && (tab[i][j - 1] == 186 || tab[i][j - 1] == 79)) ||
(direction == 'U' && tab[i][j] == 219 && (tab[i - 1][j] == 205 || tab[i - 1][j] == 79)) ||
(direction == 'D' && tab[i][j] == 219 && (tab[i + 1][j] == 205 || tab[i + 1][j] == 79)))
return 1;
}
}
return 0;
}
int main()
{
HANDLE hConsole;
hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, 241);
int tab[28][120];
int randligne = rand() % 26 + 1;
int randcols = rand() % 118 + 1;
create(tab, 28, 120, randligne, randcols);
paint(tab, 28, 120, hConsole);
char i = '1';
char direction = 'R';
int eaten = 0;
int expand = 0;
int death = 0;
while(i != 'k')
{
if (kbhit())
i = getch();
switch(i) {
case 'z':
if (direction != 'D')
direction = 'U';
break;
case 's':
if (direction != 'U')
direction = 'D';
break;
case 'd':
if (direction != 'L')
direction = 'R';
break;
case 'q':
if (direction != 'R')
direction = 'L';
break;
}
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
death = checkDeath(tab, 28, 120, direction);
if (death == 1)
break;
translate(tab, direction, 28, 120);
expand = checkExpand(tab, 28, 120, direction);
if (expand == 0)
destroyTail(tab, 28, 120);
else
{
while (tab[randligne][randcols] != ' ')
{
randligne = rand() % 26 + 1;
randcols = rand() % 118 + 1;
}
tab[randligne][randcols] = 176;
eaten++;
}
printf("Number of biscuits eaten : %d ; direction : %c ; expand : %d", eaten, direction, expand);
paint(tab, 28, 120, hConsole);
Sleep(100);
}
while(i != 'k')
{
if (kbhit())
i = getch();
}
}
Too much code to take in quickly. If you have Windows console ouput for the snake, can't you simply printf the next line and leave it at that?
If you want to hide the cursor (instead of trying to park it out of sight) you can make it invisible by calling SetConsoleCursorInfo and the struct passed is shown here.

Transposition cipher too long compilation

I need to do a program that decodes Transposition cipher. On start I have one word, which is in encrypted text. the length of the key is equal to the -1 length of the received word. key is random.
Examples:
input text:Uniz isvrh tiesiime ffrmfbi jz tpywjhpf gx ucs zgvey eoj e igpg himua xgxfx qbxo xgw b xihapbx vftx jt xik jpxq pl eo owpygfrit zvjgrhri
input word: mark
output:They could scarcely believe it possible at two yards and a half below water mark was a regular rent in the form of an isosceles triangle
input text:Qr oc cvtmxen ev Rga Asto vlg uwiuxksp acw cx kxu lgmilv
input word:was
output:On my arrival at New York the question was at its height
input text: Rt kolniz qufkbnx R gjvoczkm zqk kgobzkwin ul cnn suwyckx
input word: effect
output:In effect however I admitted the existence of the monster
For examples 1 and 2 the program compiles quite quickly, but for example 3
the program compiles for a very long time because i have too many for loops.
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
int main()
{
unsigned int i = 0;
int key[10];
unsigned int k = 0;
int check;
unsigned int j, jj, jjj, jjjj, jjjjj, jjjjjj;
int n = 50;
char slowo[10];
char plain[500], cipher[500];
fflush(stdin);
printf("Enter text:");
fgets(plain, 500, stdin);
printf("Enter word:");
fgets(slowo, 500, stdin);
if (strlen(slowo) == 4)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && isspace(cipher[i + 4]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
if (strlen(slowo) == 5)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && isspace(cipher[i + 5]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
if (strlen(slowo) == 6)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && isspace(cipher[i + 6]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
if (strlen(slowo) == 7)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && isspace(cipher[i + 7]))
{
jj--;
}
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (jjjjj = n; jjjjj != 0; jjjjj--)
{
key[4] = (jjjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && cipher[i + 6] == slowo[5] && isspace(cipher[i + 7]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
}
if (strlen(slowo) == 8)
{
for (j = n; j != 0; j--)
{
key[0] = (j - 25);
for (jj = n; jj != 0; jj--)
{
key[1] = (jj - 25);
for (jjj = n; jjj != 0; jjj--)
{
key[2] = (jjj - 25);
for (jjjj = n; jjjj != 0; jjjj--)
{
key[3] = (jjjj - 25);
for (jjjjj = n; jjjjj != 0; jjjjj--)
{
key[4] = (jjjjj - 25);
for (jjjjjj = 0; jjjjjj != 0; jjjjjj--)
{
key[5] = (jjjjjj - 25);
for (i = 0; i<strlen(plain); i++)
{
if (isalpha(plain[i]))
{
if (islower(plain[i]))
{
check = plain[i];
if (islower(plain[i]) && check + key[k]>96 && check + key[k]<123)
{
cipher[i] = (plain[i] + key[k] - 'a') % 26 + 'a';
}
else
{
check = 123 + (check - 97 + key[k]);
cipher[i] = (check - 'a') % 26 + 'a';
check = 0;
}
}
else
{
check = plain[i];
if (isupper(plain[i]) && check + key[k]>64 && check + key[k]<91)
{
cipher[i] = (plain[i] + key[k] - 'A') % 26 + 'A';
}
else
{
check = 91 + (check - 65 + key[k]);
cipher[i] = (check - 'A') % 26 + 'A';
check = 0;
}
}
}
else
if (isspace(plain[i]))
{
k--;
cipher[i] = plain[i];
}
k++;
if (k>strlen(slowo) - 3)//ostatniznak + \0
k = 0;
}
for (i = 0; i<strlen(plain); i++)
{
if (isspace(cipher[i]) && cipher[i + 1] == slowo[0] && cipher[i + 2] == slowo[1] && cipher[i + 3] == slowo[2] && cipher[i + 4] == slowo[3] && cipher[i + 5] == slowo[4] && cipher[i + 6] == slowo[5] && cipher[i + 7] == slowo[6] && isspace(cipher[i + 7]))
{
cipher[strlen(plain) - 1] = '\0';
printf("%s", cipher);
return 0;
}
}
}
}
}
}
}
}
}
else {
printf("Error");
return 1;
}
}
I would like to optimize the code to make it faster, but I have no idea.
There are some possible optimizations - what is called a keyhole optimization - in your code, but I'd rather change the algorithm you're using. Keyhole optimizations are penny wise and pound foolish, and premature optimization is the root of all evil. Decreasing the algorithm complexity is almost always your top priority. Then and only then you do further optimizations.
Here you know that key length is (in the third example) five, because the crib effect is six characters; we'll refer to the key as 12345.
We align the key to the cyphertext since whitespaces are ignored:
12 345123 4512345 1 23451234 512 345123451 23 451 2345123
Rt kolniz qufkbnx R gjvoczkm zqk kgobzkwin ul cnn suwyckx
We could now implement a brute-force Babbage decryption in O(n3) using a linear equation system:
x1 - 1 = R
x2 - 2 = t
x3 - 3 = k
x4 - 4 = o
x5 - 5 = l
x6 - 1 = n
...
x48 - 3 = x
But we can rather observe that we only have one word whose length is equal to that of 'effect' - it is 'kolniz'. Which means that we can recover the key straight away: it is the difference "nikol - efeec". The first symbol, 1, equals to a shift forward from the second e of effect to the n of kolniz, that is, 9 positions. So when we find a letter mapped to the 1 symbol of the worm, such as the first 'R', we go back 9 positions from R, and we find 'I'.
The above algorithm is roughly O(n+m) with n being the length of the ciphertext and m being the length of the key.
Suppose we have two words whose length is equal to "effect", say they are "abcdef" and "ghijkl". Which one do we use?
Hypothesis 1: abcdef is the cyphertext of effect. If that is true then the shift for the first symbol of 'e' is 'a' and the same shift must get 'f' into 't'.
Hypothesis 2: ghikl is the cyphertext of effect. If that is true then the shift for the first symbol of 'e' is 'g' and the same shift must get 'l' into 't'.
The chances of both hypotheses being true and us not being able which one is correct are those of another word having the same shift difference in its first and last letters as are 'e' and 't' in our crib, for example the first word is 'effect' and the other is "cougar", "dirges", "harlow", or "jalopy". Those chances are very little.

Converting 2D array of numbers into chars

I am making a maze using Depth-First search algorithm as a school project. The maze is working pretty much as I want but I have a little problem. I have to use # as a wall and . as a path when printing the maze but I used 0 as a wall and 1 as a path at first thinking that it is gonna be easy to change it later but I was wrong. How can I change 0 and 1 into # and .?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void nahodne_suradnice(int *r, int *s, int n)
{
srand(time(NULL));
*r = ((rand() % n) - 1) + 2;
if (*r == 1 || *r == n)
{
*s = (rand() % n) + 2;
}
else
{
if (rand() % 2 == 1)
*s = 1;
else
*s = n;
}
}
int main()
{
int i, j, n, r, s,smer,posledny_smer[1500];
int maze[500][500];
scanf_s("%d", &n);
if (n < 10 || n > 100)
{
return 0;
}
//vynulovanie pola/bludiska
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
maze[i][j] = 0;
}
}
//nahodny vyber zaciatku bludiska
nahodne_suradnice(&r, &s, n);
//generovanie bludiska
j = 0;
maze[r][s] = 2;
for (i = 0 ;; i++)
{
//backtracking
if ((maze[r - 1][s] == 1 || maze[r - 2][s] == 1 || r - 2 <=1 || s==n || s==1) && (maze[r][s + 1] == 1 || maze[r][s + 2] == 1 || s + 2 >= n || r == n || r==1) && (maze[r + 1][s] == 1 || maze[r + 2][s] == 1 || r + 2 >= n || s == n || s==1) && (maze[r][s - 1] == 1 || maze[r][s - 2] == 1 || s - 2 <=1 || r == n || r==1))
{
if (posledny_smer[j-1] == 1)
if (maze[r + 1][s] == 1 && maze[r + 2][s] == 1)
{
r += 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 2)
if (maze[r][s - 1] == 1 && maze[r][s - 2] == 1)
{
s -= 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 3)
if (maze[r - 1][s] == 1 && maze[r - 2][s] == 1)
{
r -= 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (posledny_smer[j-1] == 4)
if (maze[r][s + 1] == 1 && maze[r][s + 2] == 1)
{
s += 2;
j--;
continue;
}
else
{
j--;
continue;
}
if (j == 0)
{
if (r == n)
{
nahodne_suradnice(&r, &s,n);
maze[1][s] = 3;
maze[2][s] = 3;
}
if (r == 1)
{
nahodne_suradnice(&r, &s, n);
maze[n][s] = 3;
maze[n - 1][s] = 3;
}
if (s == n-2)
{
nahodne_suradnice(&r, &s, n);
maze[r][1] = 3;
maze[r][2] = 3;
}
if (s == 3)
{
nahodne_suradnice(&r, &s, n);
maze[r][n] = 3;
maze[r][n-1] = 3;
}
break;
}
}
//buranie stien
smer = (rand() % 4) + 1;
if (smer == 1)
{
if (r - 2 >1 && s<n && s>1)
{
if (maze[r - 1][s] == 1 || maze[r - 2][s] == 1)
continue;
maze[r - 1][s] = 1;
maze[r - 2][s] = 1;
r -= 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 2)
{
if (s + 2 < n && r < n && r>1)
{
if (maze[r][s+1] == 1 || maze[r][s+2] == 1)
continue;
maze[r][s + 1] = 1;
maze[r][s + 2] = 1;
s += 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 3)
{
if (r + 2 < n && s < n && s>1)
{
if (maze[r + 1][s] == 1 || maze[r + 2][s] == 1)
continue;
maze[r + 1][s] = 1;
maze[r + 2][s] = 1;
r += 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
if (smer == 4)
{
if (s - 2 >1 && r < n && r>1)
{
if (maze[r][s-1] == 1 || maze[r][s-2] == 1)
continue;
maze[r][s - 1] = 1;
maze[r][s - 2] = 1;
s -= 2;
posledny_smer[j] = smer;
j++;
continue;
}
}
}
//vypis bludiska
for (i = 1; i < n + 1; i++)
{
for (j = 1; j < n + 1; j++)
{
printf("%d", maze[i][j]);
}
printf("\n");
}
system("PAUSE");
return 0;
}
`
Thanks.
Change the definition of maze to:
char maze[500][500]
and change all references to use char instead of int. Then return '#' and '.' instead of 0 and 1.
You can change from 0 and 1 to # and . (or any other representation) when printing. For instance you can change:
printf("%d", maze[i][j]);
to
switch(maze[i][j]) {
case 0:
printf("#");
break;
case 1:
printf(".");
break;
default:
printf("X"); /* in case you have some value other than 0 or 1 in the maze */
}
If you do it like this, you can change which letters you want to display the maze as without changing other parts of your code.

Resources