I have a 5 x 10 char matrix and I am suppose to compress it like a run length encoding.
printf("Enter your data (5x10) of characters: \n");
for (i = 0; i < SIZE1; i++)
for (j = 0; j < SIZE2; j++)
scanf("%c", &matrix2[i][j]);
printf("\n");
compress(matrix2);
break;
void compress(char data[SIZE1][SIZE2])
{
int row, column, count=1;
for (row = 0; row < SIZE1; row++)
{
for (column = 0; column < SIZE2; column++)
{
if (data[row][column] == data[row][column + 1])
{
count++;
}
else
{
printf("%c%d", data[row][column], count);
count = 1;
}
}
}
}
This is my code but I do not understand why my output will always have a 1 in front.
Input:
aaabbbcccd
aabbccddee
aaaaaaaaaa
abcabcabca
aaabbbcccc
output:
a3b3c3d1
1a2b2c2d2e2
1a10
1a1b1c1a1b1c1a1b1c1a1
1a3b3
expected output:
a3b3c3d1
a2b2c2d2e2
a10
a1b1c1a1b1c1a1b1c1a1
a3b3c4
It is counting the newline character too.
I believe the commenters are suggesting a change like so:
printf("Enter your data (5x10) of characters: \n");
for (i = 0; i < SIZE1; i++)
for (j = 0; j < SIZE2; j++)
scanf("%c", &matrix2[i][j]);
printf("\n");
compress(matrix2);
break;
void compress(char data[SIZE1][SIZE2])
{
int row, column, count=1;
for (row = 0; row < SIZE1; row++)
{
for (column = 0; column < SIZE2; column++)
{
if (data[row][column] == data[row][column + 1])
{
count++;
}
else
{
if (data[row][column] != '\n')
{
printf("%c%d", data[row][column], count);
count = 1;
}
}
}
}
}
Change the data entry. Have it deal with \n and error detection.
printf("Enter your data (%dx%d) of characters: \n", SIZE1, SIZE2);
for (i = 0; i < SIZE1; i++) {
char buffer[(SIZE2 + 2)*2]; // Consider IO buffers 2x the needed size
if (fgets(buffer, sizeof buffer, stdin) == NULL)
Handle_EndOfFileOrIOError();
size_t len = strlen(buffer);
if (len != SIZE2+1 || buffer[len-1] != '\n')
Handle_UnexpectDataLengthError(buffer, len);
for (j = 0; j < SIZE2; j++) {
matrix2[i][j] = buffer[j];
}
// or simply memcpy(matrix2[i], buffer, SIZE2);
printf("\n");
compress(matrix2);
break;
Related
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define N_MAX 10
#define N_MIN 2
#define TEMPSIZE 1024
float RowSum(float **matrix, int sizearray,int row) {
float sum;
for (int j=0 ; j <= row; j++) {
sum = 0;
for (int i=0 ; i < sizearray; i++) {
sum = sum + matrix[j][i];
}
}
return sum;
}
float ColSum(float **matrix, int sizearray, int col) {
float sum;
for (int j = 0; j <= col; j++) {
sum = 0;
for (int i = 0; i < sizearray; i++) {
sum = sum + matrix[i][j];
}
}
return sum;
}
int RepNum(int arraysize, float **matrix) {
int i, j, counter = 0;
int temparray[N_MAX*N_MAX];
for (i = 0; i < N_MAX*N_MAX; i++) {
temparray[i] = 0;
}
for (i = 0; i < arraysize; i++) {
for (j = 0; j < arraysize; j++) {
temparray[(int)matrix[i][j]]++;
}
}
for (i = 1; i < arraysize*arraysize; i++) {
if (temparray[i] > 1)
counter++;
}
return counter;
}
void PrintArray(float **matrix, int arraysize) {
for (int i = 0; i<arraysize; i++)
{
for (int j = 0; j<arraysize; j++)
printf("%3d ", (int)matrix[i][j]);
printf("\n");
}
}
int CheckInt(float **matrix, int arraysize) {
int counter = 0;
for (int i = 0; i < arraysize; i++) {
for (int j = 0; j < arraysize; j++) {
if (((int)matrix[i][j]) != matrix[i][j])
counter++;
}
}
return counter;
}
void main() {
printf("Hello! this program will help you to find out if you have a magic square!\nPlease enter your matrix order and following it the numbers you'd like to check: \n");
float **matrix;
char input[TEMPSIZE];
int sizear = 0;
float correctsum = 0.0;
int counter = 0, row, column;
fgets(input, TEMPSIZE, stdin);
char* str = strstr(input, " ");
if (str == 0)
return;
str[0] = 0;
str++;
sizear = atof(input);
if (sizear > N_MAX || sizear < N_MIN)
{
printf("ERROR: The matrix size cannot be %d size. \n", sizear);
exit(0);
}
matrix = (float**)calloc(1, sizear * sizeof(float*));
for (column = 0; column < sizear; column++)
{
matrix[column] = (float*)calloc(1, sizear * sizeof(float));
}
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
char* temp = strstr(str, " ");
if (temp == 0) /*gets the last number*/
{
++counter;
matrix[row][column] = atof(str);
goto end;
}
if (atof(temp) <= sizear*sizear && atof(temp) > 0) { /*puts all numbers in matrix*/
temp[0] = 0;
matrix[row][column] = atof(str);
str = temp + 1;
++counter;
}
else {
printf("you cannot enter the number %.3f \n", atof(temp));
exit(0);
}
}
}
end:
if (counter > sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter < sizear*sizear) {
printf("you've entered %d numbers, while you should've enter %d numners \n", counter, sizear*sizear);
}
else if (counter == sizear*sizear) {
correctsum = (float)((sizear*(sizear*sizear + 1)) / 2);
float row = RowSum(matrix, sizear, 0);
float coul = ColSum(matrix, sizear, 0);
if (row == coul && row== correctsum && coul==correctsum && RepNum(sizear, matrix) ==0 && CheckInt(matrix,sizear)==0) {
printf("It's a magic matrix!\n");
PrintArray(matrix, sizear);
}
else {
printf("Not a magic square:\n");
if (row != coul && row != correctsum && coul != correctsum) {
printf("* Coloums and rows sum do not match.\n");
}
if (RepNum(sizear, matrix) != 0) {
printf("* There are repeating numbers.\n");
}
if (CheckInt(matrix, sizear) != 0) {
printf("* One of the numbers or more you've entered isn't integer.\n");
}
}
}
for (column = 0; column < sizear; column++)
{
free(matrix[column]);
}
free(matrix);
}
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
The exercise asked to check if the input is a magic square (sum rows=columns=diagonals), making sure there are no duplicates, only int numbers and that I don't enter more or less than sizearrayXsizearray numbers. The input from the user should look like 3 1 2 3 4 5 6 7 8 9 where the first number (here it is 3) is the array size and the rest are the numbers that would be checked as a magic square.
When I hit more than sizearrayXsizearray numbers it doesn't add into the counter and stops at sizearrayXsizearray counting. Can anyone point out why it doesn't count it?
Sure, it's simple: the counter is incremented in the loops
for (row = 0; row < sizear; row++)
{
for (column = 0; column < sizear; column++)
{
…
}
}
- the inner statements are run through (at most) sizear × sizear times, so the counter, starting from 0, cannot reach a higher value.
I'm trying to print out a hollow, open tent shape using asterisk stars "*". The code uses two for loops, the first for the rows, and the other for the columns.
following is my code:
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
} else {
for(i = 0; i < n; i++) {
for(j = 0; j < n; j++) {
printf(" ");
}
if(j == n) {
printf("*");
for(j = 1; j <= n; j++) {
printf(" ");
}
}
}
}
}
int main()
{
printTent(4);
}
Output obtained:
* * * *
Desired output:
*
* *
* *
* *
I don't think you will need that
if (n == 1) {
printf("*");
}
We can take care of that in what you've written in the else part.
For n=4, the number of spaces to be printed at the start of each line is 3, 2, 1 & 0.
You seem to be trying to accomplish that with your first inner loop. But
for(j = 0; j < n; j++) {
printf(" ");
}
will always print n spaces. We need to reduce the number of spaces printed by 1 on each iteration of the outer loop.
Coming to your second loop,
for(j = 1; j <= n; j++) {
printf(" ");
}
This has a similar problem only difference being the incrementation of the number of spaces printed.
Try something like this
void printTentNMMod(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
for(j = i; j < n; j++) {
printf(" ");
}
printf("*");
if(i!=0)
{
for(j=0; j<2*(i-1)+1; ++j)
{
printf(" ");
}
printf("*");
}
printf("\n");
}
}
Also, you could shorten this to
void printTent(int n)
{
int j;
int i;
for(i = 0; i < n; i++) {
printf("%*c", n-i, '*');
if(i!=0)
{
printf("%*c", 2*i, '*');
}
printf("\n");
}
}
The * in %*c will set the number of places occupied by the character printed by the %c.
I've finished it and I have written annotation.
void printTent(int n)
{
int j = 1;
int i = 1;
if (n == 1) {
printf("*");
}
else {
for (i = 0; i < n; i++) {
for (j = 0; j < n -i; j++) {// you should use n-i instead of n because the number of spaces is decreasing
printf(" ");
}
if (j == n-i) { //
printf("*");
for (j = 1; j <= i * 2 - 1; j++)//this loop outputs spaces between two "*"
{
printf(" ");
}
if (i != 0)//the first line only needs one "*"
printf("*");
printf("\n"); //Line breaks
}
}
}
}
Another way.
#include <stdio.h>
int main() {
int i, j;
int height = 5;
for(i = height; i > 0; i--) {
for(j = 1; j < height * 2; j++) {
if(j == i || j == height * 2 - i)
printf("*");
else
printf(" ");
}
puts("");
}
return 0;
}
Output
*
* *
* *
* *
* *
I am trying to build a dynamic maze i got to the part where i get the size and get the chars that i need to build the maze from them.
but the function thats build the maze prints it really asymmetrical how can i fix that?
my code:
#include <stdio.h>
#include <stdlib.h>
char **board;
int size = 0;
void print_Board();
void initialize_Board();
int main()
{
initialize_Board();
print_Board();
return 0;
}
/*initialize the board*/
void initialize_Board()
{
int i, j;//indexs
char s;
scanf("%d", &size);
board = (char**)malloc(sizeof(char*)* (size));
if (!board) { printf("ERROR - memroy allocation.\n"); exit(1); }
for (i = 0; i < size; i++)//for loops to build the board for the game
{
board[i] = (char*)malloc(sizeof(char)*(size));
if (!board[i]) { printf("ERROR - memroy allocation, for loop\n");
exit(1);
}
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
printf("\n");
}//for row
}
//print the board
void print_Board()
{
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
}
Change:
for (j = 0; j < size; j++)
{
scanf("%c", &s);
board[i][j] = s;
}//for col
To:
for (j = 0; j < size; j++) {
scanf("%c ", &s);
board[i][j] = s;
}//for col
board[i][j] = '\n'; // Add new line to end of row making it a string.
This ensures that each character is read and the return char is discarded.
and change:
int i, j;
for (i = 0; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%c ", board[i][j]); //print the value in the [i][j] place.
}//for col
printf("\n");
}//for row
to:
int i;
for (i = 0; i < size; i++) {
printf("%s", board[i]); //print the values in the [i] row.
}
This prints each row with a newline at the end.
I'm writing code to check whether each column in my array contains a column full of particular character (z), but there seems to be a flaw in my logic for indicating so. Are there any suggestions for modifying this?
check = 1;
for (i = 1; i < width; i++) {
for (j = 1; j <= column; j++) {
if (!((array[i+1][j] == 'z') && array[i][j] == array[i+1][j])){
check = 0;
printf("No match");
}
}
}
#include <stdio.h>
int main(void){
int width = 3, column = 3;
char array[3][3] = {
{'x','z','y'},
{'b','z','a'},
{'v','z','w'},
};
int i, j, check;
for(j = 0; j < column; j++){
check = 1;
for(i = 0; i < width; i++){
if(array[i][j] != 'z'){
check = 0;
break;
}
}
printf("columns %d is ", j+1);
if(check)
printf("match\n");
else
printf("no match\n");
}
return 0;
}
this program is trying to read in a 2D array (5x10) of char, and compress them. Such that (aaacccfeee -> a3c3f1e3). I have some trouble with my output, and I can't figure out where's the problem.
Here's the code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SIZE1 5
#define SIZE2 10
void compress(char data[SIZE1][SIZE2]);
int main ()
{
int row, col;
char qn2[SIZE1][SIZE2];
printf("\nEnter your data (5x10 of characters): \n");
for (row = 0; row < SIZE1; ++row)
for (col = 0; col < SIZE2; ++col)
scanf("%c", &qn2[row][col]);
fflush(stdin);
compress(qn2);
return 0;
}
void compress(char data[SIZE1][SIZE2])
{
int row, col, count = 0;
char tempChar = data[0][0];
printf("\nThe compression output: \n");
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c%d", tempChar, count);
tempChar = data[row][col];
count = 1;
}
} printf("\n");
}
}
Expected output:
Enter your data (5x10 of characters):
aaacccdeee
fffggghiii
jjjkkklmmm
nnnooopqqq
rrrssstuuu
The compression output:
a3c3d1e3
f3g3h1i3
j3k3l1m3
n3o3p1q3
r3s3t1u3
Output I got instead:
The compression output:
a3c3d1
e3f3g3h1
i3j3k3l1
m3n3o3p1
q3r3s3t1u3
Try this:
for (row = 0; row < SIZE1; row++)
{
for (col = 0; col < SIZE2; col++)
{
if (data[row][col] == tempChar)
{
count ++;
}
else
{
printf("%c", tempChar);
printf("%d", count);
tempChar = data[row][col];
count = 1;
}
}
}
if(count!=0) //need to print the last sequence of repeated characters.
{
printf("%c", tempChar);
printf("%d", count);
}