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;
}
Related
I would like to know how to find duplicate values in the 1st row of my 2d array.
I thought that by setting array[0][0] == array[i][j], it would check if the array[0][0] equals to the number of array[0][rest of the column]. But my code is just popping up my try again message whenever I put my first value.
Here's what I've tried so far.
void main(void)
{
int array[2][5];
int i, j, l, k;
printf("\Please enter 10 values\n");
for (j = 0; j < 5; j++)
{
for (i = 0; i < 2; i++)
{
scanf("%i", &array[i][j]);
for (k = 0; k < 2; k++)
{
for (l = 0; l < 5; l++)
{
while (array[0][0] == array[i][j])
{
printf("You entered 2 identical numbers in the first row, try again:\n");
scanf("%i", &array[i][j]);
}
}
}
}
}
}
// this isn't the fastest algorithm but it works because of the small length
int check_duplicates(int arr[], int len) {
// iterate through the array
for (int i = 0; i < len; i++) {
// only need to check to the right
// since the left elements have been checked previously
for (int j = i + 1; j < len; j++) {
if (arr[i] == arr[j]) {
// there's a duplicate, return
return 1;
}
}
}
// no duplicates found
return 0;
}
int main(void) {
int array[2][5];
int i, j, l, k;
printf("Please enter 10 values\n");
for (j = 0; j < 5; j++) {
for (i = 0; i < 2; i++) {
scanf("%i", &array[i][j]);
// a duplicate has been found
if (check_duplicates(array[0], j + 1)) {
printf("You entered a duplicate, try again.\n");
// undo one loop to read back into that position
i --;
}
}
}
return 0;
}
#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 have been trying to make this function take an array of strings and count the number of words but I keep getting 0 as my answer i.e. it hasn't counted anything. I was wondering if anyone could help me out? I believe the logic is right( i could be wrong) but I am mostly really unsure about the way I would be iterating
thank you!!
Here is my code:
int fWords (char **array, int index) {
int number = 0;
int i = 0;
int in = 0;
int j = 0;
int length = 0;
while (i < index) {
length = strlen (array[i]);
for (j = 0; array[i][j] < length; j++) {
if (isspace(array[i][j]) != 0) {
in = 0;
}
else if (in == 0) {
in = 1;
number++;
}
}
i++;
}
return number;
}
You need to reset in after every run of the inner loop. Something like this
while (i < index) {
length = strlen (array[i]);
in = 0;
//^^^^^^^
for (j = 0; j < length; j++) {
if (isspace(array[i][j]) != 0) {
in = 0;
}
else if (in == 0) {
in = 1;
number++;
}
}
i++;
}
This condition is incorrect:
for (j = 0; array[i][j] < length ; j++)
// ^^^^^^^^^^^^^^^^^^^^
it should be simply
for (j = 0; j < length ; j++)
// ^^^^^^^^^^
This will fix the problem with zero.
The part that is commented 'recurring characters code' is a bit flawed as later on when I try to print the letter and number of occurrences, the letter is correct, but the number of occurrences is some random number. Can somebody explain where my logic is faulty?
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int main()
{
//declare variables
char arr[MAX], ch;
int counter1 = 0, counter2 = 0, i, j, temp, mostcommon[128], x = 0, y = 0;
//ask for user input until array reaches MAX or is interrupted by the 'X' character and append arr[i]
for(i = 0; i < MAX; i++)
{
printf("Input %d. character into array: ", i + 1);
ch = getchar();
if (ch == 'X'){
break;
}
getchar();
arr[i] = ch;
counter1++;
}
//recurring characters code
for (i = 0; i < 128; i++){
mostcommon[i] = 0;
}
x = mostcommon[0];
y = 0;
for (i = 0; i < counter1; i++)
{
mostcommon[(int) arr[i]] += i;
}
for (i = 0; i < 128; i++){
if (x < mostcommon[i]){
x = mostcommon[i];
y = i;
}
}
//print array as it was appended
printf ("\nArray:");
for (i = 0; i < counter1; i++)
{
printf("\narray[%d] = %c", i, arr[i]);
}
//sort array by descending ASCII value
for(i = 0; i < counter1 - 1; i++)
{
for(j = i + 1; j < counter1; j++)
{
if (arr[i] < arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
//print sorted array
printf ("\n\nSorted array:");
for (i = 0; i < counter1; i++)
{
printf("\narray[%d] = %c", i, arr[i]);
}
//print array without reoccuring characters
printf ("\n\nFinal array:");
for (i = 0; i < counter1; i++)
{
if (arr[i] != arr[i-1]){
printf("\narray[%d] = %c", counter2, arr[i]);
counter2++;
}
}
printf("\n\nThe most common character is %c and it recurred %d times", y, x);
return 0;
}
One note before:
*Shouldn't you raise counter1 at the start of the loop ? Because if you don't - when X is entered it won't raise counter1 and I supposed you want number of characters entered.
Now to the code - first in the loop:
for (i = 0; i < counter1; i++)
{
mostcommon[(int) arr[i]] += i;
}
What was your purpose? If you want the number of occurrences of the character then the loop should look like this:
for(i = 0 ; i < counter1 ; i++)
{
mostcommon[(int)arr[i]]++;
}
as "+= i" has no purpose according to my understanding - so it would be kind of random as you describe. Hope it helped.
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;