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.
Related
I was doing my homework when I ran into a problem. The meaning of the program is that I set the size of a 2d array, then I write the directions in which the "turtle" will move, then the program outputs it. But when I run the program, even without instructions, the terminal outputs the following for me, for example
I can't figure out what my mistake is, by default the program should write
without that `o' at the beginning, how can I fix it?
My code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int hor;
int vert;
int x = 0;
int y = 0;
int i;
int a;
char symbol = 'z';
scanf("%d%d", &vert, &hor);
char* pole = (char*)malloc(vert * hor * sizeof(char));
for (i = 0; i < vert; i++)
{
for(a = 0; a < hor; a++)
pole[i * vert + a] = '.';
}
pole[x *vert + y] = 'o';
while(symbol != 'x')
{
scanf("%c", &symbol);
if (symbol == '^') x--;
else if (symbol == 'v') x++;
else if (symbol == '>') y++;
else if (symbol == '<') y--;
else if (symbol == 'o') pole[x *vert + y] = 'o';
if(y < 0)
y = vert - 1;
else if(y > vert)
y = 0;
if(x < 0)
x = hor - 1;
else if(x > hor)
x = 0;
}
for (i = 0; i < vert; i++)
{
for(a = 0; a < hor; a++)
{
printf("%c", pole[i *vert + a]);
}
printf("\n");
}
free(pole);
return 0;
}
I tried to remove the "o", but nothing helped me:(
**
Hello :)
So I have this task to write a program that will convert int to arg using malloc and well it all works fine, gdb shows no errors, but it is not printing any output in this form. If i delete itoa_buffer[i] = '\0'; than sometimes it shows output sometimes not. I do not know what is wrong here, seems fine.
I do not know where to ask for help as I like to follow my logic and find errors here instead of copying solutions from the internet. I willl appreciate some tips, its probably some small thing Id do not know and wont let me go further.**
#include<stdio.h>
#include "libft.h"
char *ft_itoa(int n)
{
int i;
int z;
char x;
char *itoa_buffer;
if (n == INT_MIN)
return(ft_strdup("-2147483648"));
if (n < 0)
{
n = -n;
z = n;
i = 1;
}
else
{
z = n;
i = 0;
}
while(z > 0)
{
z = z/10;
i++;
}
if(!(itoa_buffer = (char *)malloc((i+1)*sizeof(char))))
return(0);
i = i + 1;
while(--i)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 2)
{
i--;
itoa_buffer[i] = '-';
i--;
break;
}
}
itoa_buffer[i] = '\0'; // it stopped showing answers when i tried to add this symbol at the end.
return(itoa_buffer);
}
int main()
{
int n;
n = 1980;
printf("%s", ft_itoa(n));
}
You are putting '\0' as the first character of the string.
Instead of that, you should put that as the last character.
Instead of this part
i = i + 1;
while(--i)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 2)
{
i--;
itoa_buffer[i] = '-';
i--;
break;
}
}
itoa_buffer[i] = '\0';
you should do this
itoa_buffer[i] = '\0';
while(i--)
{
x = (n%10 + '0');
itoa_buffer[i] = x;
n = n/10;
if(n == 0 && i == 1)
{
i--;
itoa_buffer[i] = '-';
break;
}
}
In solving this readability problem, I have been getting some weird unexpected results(expected Grade 16+, getting Grade 10 etc etc), I am not being able to figure out where the bug is or how can I solve it, please help me figure out the bug. The codes are as follows:
//includes
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
//global variables
int lc; //letter count
int wc; //word count
int sc; //sentence count
bool awc; //already word counted
double L; //average number of letters per 100 words
double S; //average number of sentences per 100 words
float index;
//function declaration
int count_letters(string x);
int count_words(string x);
int count_sentences(string x);
//main
int main(void)
{
string text = get_string("text : ");
count_letters(text);
printf("%i letters\n", lc);
count_words(text);
printf("%i words\n", wc);
count_sentences(text);
printf("%i sentences\n", sc);
L = lc / wc * 100.0f;
S = sc / wc * 100.0f;
index = (0.0588 * L) - (0.296 * S) - 15.8;
if(index < 1)
{
printf("Before Grade 1\n");
}
else if(index >= 16)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", (int) round(index));
}
}
//functions
int count_letters(string x)
{
lc = 0;
for(int i = 0, n = strlen(x); i < n; i++)
{
if((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
{
lc += 1;
}
}
return lc;
}
int count_words(string x)
{
wc = 0;
awc = false;
for(int i = 0, n = strlen(x); i < n; i++)
{
if((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
{
if(awc == false)
{
wc += 1;
awc = true;
}
}
if(x[i] == ' ')
{
awc = false;
}
}
return wc;
}
int count_sentences(string x)
{
sc = 0;
for(int i = 0, n = strlen(x); i < n; i++)
{
if(x[i] == '.' || x[i] == '!' || x[i] == '?')
{
sc += 1;
}
}
return sc;
}
The number of letters, words and sentences from these functions are correct so far so I think the problem lies in the main section, probably something to do with the variable type of "L" and "S" or the index formulae, please help me figure out where the problem is. Thank you
here are some of the tests: sentences(expected results)
1.One fish. Two fish. Red fish. Blue fish. (Before Grade 1)
2.Harry Potter was a highly unusual boy in many ways. For one thing, he hated the summer holidays more than any other time of year. For another, he really wanted to do his homework, but was forced to do it in secret, in the dead of the night. And he also happened to be a wizard. (Grade 5)
3.A large class of computational problems involve the determination of properties of graphs, digraphs, integers, arrays of integers, finite families of finite sets, boolean formulas and elements of other countable domains. (Grade 16+)
L = lc / wc * 100.0f;
wrong
L = 100.0f * lc / wc;
right.
When both operands of / are integers, the result is integer as well, so 5/3 == 1.
the OPs code contains several problems, as discussed in the comments to the question.
The following proposed code:
corrects those problems exposed in the comments
cleanly compiles
strongly suggest all those 'global' variables be moved to inside the appropriate functions and when needed else where be passed as parameters.
and now, the proposed code:
//includes
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <math.h>
//global variables
int lc; //letter count
int wc; //word count
int sc; //sentence count
bool awc; //already word counted
double L; //average number of letters per 100 words
double S; //average number of sentences per 100 words
double i;
//function declaration
int count_letters(string x);
int count_words(string x);
int count_sentences(string x);
//main
int main(void)
{
string text = get_string("text : ");
count_letters(text);
printf("%i letters\n", lc);
count_words(text);
printf("%i words\n", wc);
count_sentences(text);
printf("%i sentences\n", sc);
L = lc / wc * 100.0;
S = sc / wc * 100.0;
i = (0.0588 * L) - (0.296 * S) - 15.8;
if(i < 1.0)
{
printf("Before Grade 1\n");
}
else if(i >= 16.0)
{
printf("Grade 16+\n");
}
else
{
printf("Grade %i\n", (int) round(i));
}
}
//functions
int count_letters(string x)
{
lc = 0;
for( size_t i = 0, n = strlen(x); i < n; i++)
{
if((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
{
lc += 1;
}
}
return lc;
}
int count_words(string x)
{
wc = 0;
awc = false;
for( size_t i = 0, n = strlen(x); i < n; i++)
{
if((x[i] >= 'a' && x[i] <= 'z') || (x[i] >= 'A' && x[i] <= 'Z'))
{
if(awc == false)
{
wc += 1;
awc = true;
}
}
if(x[i] == ' ')
{
awc = false;
}
}
return wc;
}
int count_sentences(string x)
{
sc = 0;
for( size_t i = 0, n = strlen(x); i < n; i++ )
{
if(x[i] == '.' || x[i] == '!' || x[i] == '?')
{
sc += 1;
}
}
return sc;
}
I am trying to make a reverse Polish printer on an online coding website which can perform the following operation-
Inputs:
(a+(b*c))
((a+b)*(z+x))
((a+t)*((b+(a+c))^(c+d)))
Outputs:
abc*+
ab+zx+*
at+bac++cd+^*
This is my code:
#include <stdio.h>
#include <string.h>
char pop(int t);
void push(int c, int t);
char a[100][400];
int main()
{
int z;
scanf("%d", &z);
int i = 0;
int q = z;
while (q-- > 0)
{
scanf("%s",&a[i][0]);
int t;
for (t = 0; t < strlen(a[i]); t++) //loop to put the values and signs in the 2 stacks
{
if ((a[i][t] == '*') || (a[i][t] == '+') || (a[i][t] == '-') || (a[i][t] == '^'))
{
push(a[i][t], 2);
}
else if (a[i][t] == ')')
{
int y = pop(2);
push(y, 1);
}
else
{
push(a[i][t], 1);
}
}
int k = 0;
char c;
while ((c = pop(1)) !='\0') //loop to put elements in the array v
{
if (c != '(')
{
a[i][k++] = c;
}
}
a[i][k--] = '\0';
int m;
for (m=0; m != k; m++, k--)
{
char t = a[i][m];
a[i][m] = a[i][k];
a[i][k] = t;
}
}
int p;
for (p = 0; p <z ; p++)
printf("%s\n",a[i]);
return 0;
}
char ac[400];
char as[400];
int ic = 0;
int is = 0;
void push(int c,int t)
{
if (t == 1 && ic != 400)
ac[ic++] = c;
else if (t == 2 && is != 400)
as[is++] = c;
}
char pop(int t)
{
if (t == 1 && ic != 0)
return ac[--ic];
if (t == 2 && is != 0)
return as[--is];
return '\0';
}
On compiling this code I am getting a SIGSEGV error.I don't know whats the mistake in this code.Please help
I don't know why you are getting SIGSEGV. However, I see couple of errors in your code.
You are using scanf("%s",&a[i][0]); but i is never changed from its initial value of 0. I suggest changing
while (q-- > 0)
to
for ( i = 0; i < z; ++i )
You are printing a[i] even though you are using p as the for loop index in
for (p = 0; p <z ; p++)
printf("%s\n",a[i]);
That's easily fixed by changing the second line to:
printf("%s\n",a[p]);
In your input, you didn't include the value of z. I hope that is an error in creating the post and that you have the value of z in your input.
With those changes, I don't see any problem in my environment. I tested using gcc 4.7.3. Not sure fixing those will fix the problem in your environment.
This is a question from K&R:-
Write a program to print a histogram of the lengths of words in its input.It is easy to draw the histogram with bars horizontal; but a vertical orientation is more challenging.
I am not supposed to use any library functions because this is only a tutorial introduction!
I have written the following program to do so but it have some bugs:-
1)If there is more than one white-space character between words, the program doesn't work as expected.
2)How do I know the maximum value of 'k' I mean how to know how many words are there in the input?
Here is the code:-
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
#define MAX_WORDS 100
int main(void)
{
int c, i=0, k=1, ch[MAX_WORDS] = {0};
printf("enter the words:-\n");
do
{
while((c=getchar())!=EOF)
{
if(c=='\n' || c==' ' || c=='\t')
break;
else
ch[i]++;
}
i++;
}
while(i<MAX_WORDS);
do
{
printf("%3d|",k);
for(int j=1;j<=ch[k];j++)
printf("%c",'*');
printf("\n");
k++;
}
while(k<10);
}
This program will work fine even if there are more than one newline characters in between the two words and numWords will give you the numbers of words.
#include <stdio.h>
#include <stdbool.h>
int main(void)
{
int ch, cha[100] = {0}, k = 1;
int numWords = 0;
int numLetters = 0;
bool prevWasANewline = true; //Newlines at beginning are ignored
printf("Enter the words:-\n");
while ((ch = getchar()) != EOF && ch != '\n')
{
if (ch == ' ' || ch == '\t')
prevWasANewline = true; //Newlines at the end are ignored
else
{
if (prevWasANewline) //Extra nelines between two words ignored
{
numWords++;
numLetters = 0;
}
prevWasANewline = false;
cha[numWords] = ++numLetters;
}
}
do
{
printf("%3d|",k);
for(int j=0;j<cha[k];j++)
printf("%c",'*');
printf("\n");
k++;
} while(k <= numWords);
return 0;
}
Its kind of an old thread, but i had the same problem. The code above works like charm, but its kind of an overkill for the knowledge we have with K&R in the first 20 pages.
I had no idea what they meant by histogram printing and that code helped me with that.
Anyway i wrote a code myself with the knowledge i gained through the book, i hope it will be helpful to someone.
Forgive me if its a bit messy, i am just a beginner myself :D
#include <stdio.h>
#define YES 1
#define NO 0
int main(void)
{
int wnumb [100];
int i, inword, c, n, k;
n = (-1);
for (i = 0; i <=100; ++i) {
wnumb [i] = 0;
}
inword = NO;
while ((c = getchar()) != EOF) {
if ( c == ' ' || c == '\n' || c == '\t' ) {
inword = NO;
}
else if (inword == NO) {
++n;
++wnumb [n];
inword = YES;
}
else {
++wnumb [n];
}
}
for (i = 0; i <= 100; ++i) {
if (wnumb [i] > 0) {
printf ("\n%3d. | ", (i+1));
for (k = 1; k <= wnumb[i]; ++k) {
printf("*");
}
}
}
printf("\n");
}
Here is example of simple Histogram(Vertically)
#include <stdio.h>
int main()
{
int c, i, j, max;
int ndigit[10];
for (i = 0; i < 10; i++)
ndigit[i] = 0;
while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
max = ndigit[0];
for (i = 1; i < 10; ++i) /* for Y-axis */
if (max < ndigit[i])
max = ndigit[i];
printf("--------------------------------------------------\n");
for (i = max; i > 0; --i) {
printf("%.3d|", i);
for (j = 0; j < 10; ++j)
(ndigit[j] >= i) ? printf(" X ") : printf(" ");
printf("\n");
}
printf(" ");
for (i = 0; i < 10; ++i) /* for X-axis */
printf("%3d", i);
printf("\n--------------------------------------------------\n");
return 0;
}