Output variables becomes 0. Possible logic error - c

I declare and then assign 'n' to the variables 'm' and 'num'. By the time they reach the end of the program they end up zero.
This program is works to find the Phi Totient function of n. It all works flawlessly until the very last while loop.
int factorization(int n)
{
int i, j=0, a[14], index=0, m, num;
m=n;
num=n;
for(i=2; i<sqrt(n)+1; i++)
{
if(n%i == 0)
{
n=n/i;
if(a[0]!=i && a[1]!=i && a[2]!=i && a[3]!=i && a[4]!=i && a[5]!=i && a[6]!=i && a[! =i && a[8]!=i && a[9]!=i && a[10]!=i && a[11]!=i && a[12]!=i && a[13]!=i && a[14]!=i)
{
a[index]=i;
index++;
}
i=1;
}
}
a[index]=n;
for (i=index+1; i<=14; i++)
{
a[i]=0;
}
for (i=0; i<=10; i++)
{
printf("%d\n",a[i]);
}
while(a[j] != 0 && a[j] != 1)
{
m=m*((a[j]-1)/a[j]);
j++;
}
printf("Phi of %d = %d", num, m);
return 0;
}

Change m to double and loop to
while (a[j] != 0 && a[j] != 1)
{
m = m * ( ((double)a[j] - 1.0) / (double)a[j] );
j++;
}
If m is integer when ever the division
((a[j]-1)/a[j])
is less than 1 it is rounded down to zero and the multiplication becomes
m = m * 0

int i, j=0, a[14], index=0, m, num;
a array is not initialized but the array elements are read in:
if(a[0]!=i && a[1]!=i && a[2]!=i && a[3]!=i && a[4]!=i && a[5]!=i && a[6]!=i && a[! =i && a[8]!=i && a[9]!=i && a[10]!=i && a[11]!=i && a[12]!=i && a[13]!=i && a[14]!=i)
Morever in the above line you are testing a[14]!=i but the last element of a is a[13]. a[14] is out of the array.
Same in:
for (i=index+1; i<=14; i++)
{
a[i]=0;
}
You are accessing an element (a[14]) outside the array.

Related

Segmentation fault when there is just one value as input

My code seems to be working fine, but I get a Segmentation fault when there is just one value as input. It should print a square shape based on a number as character input.
test case : ["2", "2"]
"oo\noo\n"
test case: ["", ""]
""
test case : ["2"]
SIGSEGV (signal 11)
#include <string.h>
#include <stdlib.h>
void my_square(int *x, int *y)
{
int i , j;
if (*x == 0 || *y == 0) {
printf("");
}
else{
for(i = 0; i < *x; i++){
for(j = 0; j < *y; j++){
if(*x<=2 && j == 0){
printf("o");
}else if(*x<=2 && j == 1){
printf("o\n");
}else if(*y<=2 && i == 0){
printf("o");
}else if(*y<=2 && i == 1){
printf("o\n");
}else{
//printf(" i: %d, j: %d ", i, j);
if(i == 0 && j == 0 || i == *y-1 && j == 0 || i == 0 && j == *x-1 || i == *y-1 && j == *x-1){
printf("o");
}
if(i >= 1 && j == 0 && i != *y-1) {
printf("|");
}
if(i >= 1 && j == *x-1 && i != *y-1) {
printf("|");
}
if(i == 0 && j >= 1 && j != *y-1|| i == *x-1 && j >= 1 && j != *y-1){
printf("-");
}
if(i >= 1 && j >= 1 && i < *x-1 && j < *y-1){
printf(" ");
}
if(j == *x-1){
printf("\n");
}
}
//printf("> %d, %d", i, j);
}
}
}
}
int main(int ac, char **av)
{
int x = atoi(av[1]);
int y = atoi(av[2]);
my_square(&x, &y);
return 0;
}```
You should always check ac before accessing av, otherwise it may lead to undefined behaviour (and cause a segmentation fault).
That's how you could do it (the first value is always the program file name):
int main(int ac, char **av)
{
int x, y;
if (ac <= 3)
{
x = atoi(av[1]);
y = x; // if there's only one argument, we use it for both x and y
if (ac == 3)
{
y = atoi(av[2]);
}
my_square(&x, &y);
}
return 0;
}

Why atof function does not work properly?

I am coding in C in a university course and we got a project to take equations from the user and give solutions for matrices etc...
My problem is that I am trying to use atof() function and for a reason I can't find in the same loop once it works and the other times it doesn't.
I have tried already other functions to replace atof like strtod but it doesn't work as well.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cstdbool>
void main()
{
int num, check = 0,i,j,k=0,len1=0;
char equ[80],tempx[20],tempy[20], tempz[20], tempd[20];
double *x, *y, *z, *d;
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
getchar();
while (check == 0) //a check to see if we got a number between 1-3.
{
if (num > 0 && num < 4)
check = 1;
else
{
printf_s("Please enter a number between 1-3.\n");
printf_s("Number of equations (1-3): ");
scanf_s("%d", &num);
}
}
x = malloc(sizeof(double)*num);
if (!x) exit(1);
y = malloc(sizeof(double)*num);
if (!y) exit(1);
z = malloc(sizeof(double)*num);
if (!z) exit(1);
d = malloc(sizeof(double)*num);
if (!d) exit(1);
for (i = 0; i < num; i++) //getting the equations and putting them into the matrix
{
printf_s("Enter equation %d: ", i + 1);
gets_s(equ, sizeof(equ));
len1 = strlen(equ);
for (j = 0; j <len1 ; j++)
{
if (equ[j] == 'x')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j-k>=0)
{
tempx[j-k] = equ[j-k];
k++;
}
x[i] = atof(tempx);
}
else if (equ[j] == 'y')
{
k = 0;
while ((equ[j-k] != '+' || equ[j-k] != '-') && j - k >= 0)
{
tempy[j-k] = equ[j-k];
k++;
}
y[i] = atof(tempy);
}
else if (equ[j] == 'z')
{
k = 0;
while ((equ[j - k] != '+' || equ[j - k] != '-') && j - k >= 0)
{
tempz[j-k] = equ[j - k];
k++;
}
z[i] = atof(tempz);
}
else if (equ[j] == '=')
{
k = 0;
while (equ[j+k])
{
tempd[k] = equ[j + k];
k++;
}
d[i] = atof(tempd);
}
}
}
free(x);
free(y);
free(z);
free(d);
}
I expected to get the same result in d[i] as I did in x[i] but every time I try to print d[i] I get 0.0000. When I tried the function _strrev on tempd inside atof I got the reverse result inside d[i].
So the problem was that in the last loop i inserted a string that start with "=" and not a number. Apparently atof() doesn't work when the first char is not a number.

How to extend my multiple loop conditions?

My code
#include <stdlib.h>
#include <stdio.h>
int main()
{
int i=0;
int j=0;
size_t count=0;
float numbers[20][100];
float velocity[21][101];
char *line = NULL;
FILE *myFile;
myFile = fopen("vel.txt", "r");
if (myFile == NULL)
{
printf("Error Reading File\n");
exit (0);
}
while(i < 20 && getline(&line, &count, myFile)!=-1) {
int len = 0, pos = 0;
j = 0;
while(j < 100 && 1 == sscanf(line + pos, "%f%n", &numbers[i][j++], &len))
pos += len;
i++;
}
free(line);
fclose(myFile);
i=1;
for( j = 0; j < 101; j++ )
{
if( j == 1 )
{
velocity[i][j]=numbers[i][j];
}
else if ( j == 101 )
{
velocity[i][j]=numbers[i][j];
}
else
{
velocity[i][j]=(numbers[i][j-1]+numbers[i][j])/2;
}
}
for (j=0 ; j<101 ; j++) {
printf("\n%f", velocity[i][j]);
}
}
I need to calculate velocities for 21,101 two dimensional mesh.If i==1 ,that is my code above and works fine.The sam conditions apply if i==21.But for all other values (2 to 20) calculations are different.How should I change
if( i== from 2 to 20 &&j == 1 )
{
do something
}
else if (i== from to to 20 && j == 101 )
{
do something 2
}
else(means i goes from 2,20 j goes from 2,100)
{
do something 3
}
Do you want something like this: if(i >= 2 %% i <= 20)? Means: 2 <= i <= 20 or if i is greater or the same as 2 and i is lower or the same as 20 it is true.
If your example:
if(i >= 2 && i <= 20 && j == 1)
{
//do something
}
else if(i >= 2 && i <= 20 && j == 101)
{
//do something 2
}
else if(i >= 2 && i <= 20 && j >= 2 && j <= 100) //means i goes from 2,20 j goes from 2,100
{
//do something 3
}
or is there anything I missed?

Wrong output from an array

I've been running this for a couple of hours, and continue to get the wrong output, and can't seem to find out why. It seems as if everything should work, but I consistently get a weird character for the first time I implement this, and the tokens are not getting moved around in the way that they should be. This is just practice code, to be implemented in assembly language.
char get_line(void){
//Char array, buf space 80, int array, hold the numerical value,
char arr[80];
int int_arr[80];
char arr_print[80];
//Two points to compare whether the value in the given array changed.
int compare;
int compare_2;
//Array points, indexes and size counter.
int count = -1;
int i = 0;
int j = 0;
int k;
gets(arr);//Unsafe version of code, but for this implementation negligible.
while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
//Runs through and sets the value based on specs, #'s =1, alpha =2, ...
//For the comparison with the below code.
if(isalpha(arr[i])){
int_arr[i] = 2;// printf("%c: 2", arr[i]);
compare = 2;
}else if(isdigit(arr[i])){
int_arr[i] = 1;// printf("%d: 1", arr[i]);
compare = 1;
}else if(arr[i] == '$'){
int_arr[i] = 5;// printf("%c: 5", arr[i]);
compare = 5;
}else if(arr[i] == '#'){
int_arr[i] = 6;// printf("%c: 6", arr[i]);
compare = 6;
}else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' ||
arr[i] == '.' || arr[i] == ':'){
int_arr[i] = 4;// printf("%c: 4", arr[i]);
compare = 4;
}else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' ||
arr[i] == '/'){
int_arr[i] = 3;//printf("%c: 3", arr[i]);
compare = 3;
}else if(isspace(arr[i]))
int_arr[i] = 5;//Ignore the spaces in this implementation.
/*
Runs the comparison point to assure that the
tokens are matched up and grouped as needed.
*/
if(compare_2 == 0 || (compare != compare_2)){
if(compare_2 != 0){
for(k=0; k<=j ;k++)
printf("%c", arr_print[k]);
j=0;
}
printf("\t\t%d \n", compare_2);
compare_2 = compare;
}else if( isspace(arr[i]) == 0 ){
arr_print[j] = arr[i];
// printf("\t\t\t\t\t%c | %d\n", arr_print[j],j);
j++;
}
i++;
count++;
}
printf("\n\n");
//Code for previous implementation in C
for(i=0; i<80 && arr[i] != NULL; i++)
printf("%c", arr[i]);
printf("\n");
for(i=0; i< count+1; i++)
printf("%d", int_arr[i]);
printf("\n");
if(i == 0 || count == -1) return '#';
return arr[count];
}
I think this is what you needed
Try this code
char get_line(void)
{
//Char array, buf space 80, int array, hold the numerical value,
char arr[80];
int int_arr[80];
char arr_print[80];
//Two points to compare whether the value in the given array changed.
int compare=0;
int compare_2=0;
//Array points, indexes and size counter.
int count = -1;
int i = 0;
int j = 0;
int k = 0;
int l = 0; // I add an int l for
gets(arr);//Unsafe version of code, but for this implementation negligible.
//while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
while( i < 80 && arr[i] != '\0')
{
//Runs through and sets the value based on specs, #'s =1, alpha =2, ...
//For the comparison with the below code.
if(isalpha(arr[i]))
{
int_arr[i] = 2;// printf("%c: 2", arr[i]);
compare = 2;
}
else if(isdigit(arr[i]))
{
int_arr[i] = 1;// printf("%d: 1", arr[i]);
compare = 1;
}
else if(arr[i] == '$')
{
int_arr[i] = 5;// printf("%c: 5", arr[i]);
compare = 5;
}
else if(arr[i] == '#')
{
int_arr[i] = 6;// printf("%c: 6", arr[i]);
compare = 6;
}
else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || arr[i] == '.' || arr[i] == ':')
{
int_arr[i] = 4;// printf("%c: 4", arr[i]);
compare = 4;
}
else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' || arr[i] == '/')
{
int_arr[i] = 3;//printf("%c: 3", arr[i]);
compare = 3;
}
else if(isspace(arr[i]))
int_arr[i] = 5;//Ignore the spaces in this implementation.
/*
Runs the comparison point to assure that the
tokens are matched up and grouped as needed.
*/
if(compare_2 == 0 || (compare != compare_2))
{
if(compare_2 != 0)
{
for(k=0; k<=j ;k++)
printf("%c", arr[l+k]); // arr_print replaced by arr
j=0;
l+=k; // int l to go through array arr
}
printf("\t\t%d \n", compare_2);
compare_2 = compare;
}
else if( isspace(arr[i]) == 0 )
{
arr_print[j] = arr[i];
//printf("\t\t\t\t\t%c | %d\n", arr_print[j],j);
j++;
}
i++;
count++;
}
printf("%c", arr[count]); // Repeated code to print the last element
printf("\t\t%d \n", compare_2);
compare_2 = compare;
printf("\n\n");
//Code for previous implementation in C
for(i=0; i<80 && arr[i] != NULL; i++)
printf("%c", arr[i]);
printf("\n");
for(i=0; i< count+1; i++)
printf("%d", int_arr[i]);
printf("\n");
if(i == 0 || count == -1) return '#';
return arr[count];
}

Trouble with moving tiles in Game of Fifteen

Below is a function in the game of fifteen which should search for the "0" tile and once found, allow adjacent tiles to move into its place. It works for the first few moves but then doesn't allow moves that it should permit when the zero tile is at the top row (it starts on the bottom right). Note below the "0" tile is drawn as an underscore. For example:
8 7 6
5 4 3
2 1 _
Tile to move: 3
8 7 6
5 4 _
2 1 3
Tile to move: 6
8 7 _
5 4 6
2 1 3
Tile to move: 7
Illegal move.
Here is the code:
bool move(int tile)
{
int blankrow;
int blankcol;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if (board[i][j] == 0)
{
blankrow = i;
blankcol = j;
}
}
}
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
if ((board[i][j] == tile) && ((board[i+1][j] == 0 || board[i-1][j] == 0) &&
(board[i][j+1] == 0 || board[i][j-1] == 0)))
{
int swapped = board[i][j];
board[blankrow][blankcol] = swapped;
board[i][j] = 0;
return true;
}
}
}
return false;
}
Just by looking, I'm pretty sure you need to change:
((board[i+1][j] == 0 || board[i-1][j] == 0) &&
(board[i][j+1] == 0 || board[i][j-1] == 0))
To:
(board[i+1][j] == 0 || board[i-1][j] == 0 ||
board[i][j+1] == 0 || board[i][j-1] == 0)
EDIT: I agree with the user comment below. Better code would look something like:
bool move(int tile)
{
int blankrow, blankcol, tilerow, tilecol;
for (int i = 0; i < d; i++) {
for (int j = 0; j < d; j++) {
if (board[i][j] == 0) {
blankrow = i;
blankcol = j;
}
else if (board[i][j] == tile) {
tilerow = i;
tilecol = j;
}
}
}
if( (blankrow == tilerow && abs(blankcol - tilecol) == 1) ||
(blankcol == tilecol && abs(blankrow - tilerow) == 1) ) {
board[blankrow][blankcol] = board[tilerow][tilecol];
board[tilerow][tilecol] = 0;
return true;
}
return false;
}
You found the blankrow and blankcol. Do the same thing to find the tilerow and tilecol. Then verify either
((blankrow == tilerow) && (abs(blankcol - tilecol) == 1))
or
((blankcol == tilecol) && (abs(blankrow - tilerow) == 1))
Swap if either of those conditions is met. The problem with the existing code is that you can have array accesses out-of-bounds. For example, if i is 0, then board[i-1][j] is an out-of-bounds access.
I think you should change the if statement in the second loop from
if ((board[i][j] == tile) &&
((board[i+1][j] == 0 || board[i-1][j] == 0) &&
(board[i][j+1] == 0 || board[i][j-1] == 0)))
to
if ((board[i][j] == tile) &&
((i+1==blankrow && j==blankcol) ||
(i-1==blankrow && j==blankcol) ||
(i==blankrow && j+1==blankcol) ||
(i==blankrow && j-1==blankcol)))
In the original you are requiring a row offset of one and a column offset of zero and a row offset of zero and a column offset of one simultaneously. Also you're potentially taking a negative index into the array or reading past its end.

Resources