Hello im in need of some help this part of my program is to get a input string like 2x³+2y² and separate it in 2 arrays termos=terms and exp=exponential, however i cant seem to get it working
#include <stdio.h>
#include <math.h>
int main()
{
char poly[50];
int termos[10];
int exp[10];
int contt=0, conte=0, i=0;
char var1, var2, var3;
printf("Introduza o polinómio\n");
scanf("%s", &poly);
for(i=0; i<50; i++)
{
if(poly[i-1]==char && poly[i]==int && poly[i-1]!='+')
{
exp[conte]=poly[i];
conte++;
}
if(poly[i]==int)
{
termos[contt]=poly[i];
contt++;
}
if(poly[i]=='x')
var1=poly[i];
if(poly[i]=='y')
var2=poly[i];
if(poly[i]=='z')
var3=poly[i];
}
Simply impossible, there is not runtime type information in c. Perhaps you should read The XY Problem and ask another question later.
In the line
if(poly[i-1]==char && poly[i]==int && poly[i-1]!='+')
you want to know if poly[i-1] is an alphabetic character, e.g. an 'a', or is a number. You can use the following functions from ctype.h for that:
// among the includes
#include <ctype.h>
// later
if(isalpha(poly[i-1]) && isdigit(poly[i]) && poly[i-1]!='+')
There are more problems in your code. We will post these as comments.
Related
The program runs and exits with code 0, but gives no output, it's supposed to be a linear search program
I looked to other similar problems, i tried to end the array with \n. tried instead of just relying in just the "if (strcmp=0)" to make something with the values strcmp return, I'm very new and for what I'm learning not very good, just made things worst, i tried to look if it was about the char* values strcmp expect, but couldn't find the problem
#include <stdio.h>
#include <string.h>
#define max 15
int lineal(char elementos[], char elebus)
{
int i = 0;
for(i=0; i<max; i++)
{
if(strcmp(elementos[i], elebus)==0)
{
printf("Elemento encontrado en %d,", i); //element found in
}
else
{
printf("elemento no encontrado"); //not found
}
}
}
int main()
{
char elebus[50];
char elementos[max][50]= {"Panque", "Pastel", "Gelatina", "Leche", "Totis", "Tamarindo" "Papas", "Duraznos", "Cacahuates", "Flan", "Pan", "Yogurt", "Café", "Donas", "Waffles"};
printf("Escribir elemento a buscar\n");
scanf("%s", elebus);
int lineal(char elementos[], char elebus);
}
The expected output would be element found in "i" position, if found
if not found print "not found"
You want to pass it a string to find, not just one character Also, elementos should be a 2D array. Change the signature of your function to this:
int lineal(char elementos[max][50], char *elebus)
Also, in main, you don't call the function. Instead, you just declare it again. call it like this:
lineal(elementos, elebus);
Furthermore, I would change it to return void instead of int. You're neither returning anything (that's undefined behavior) nor are you using the return value anywhere. But I assume that this isn't the final version and you want to return the index at some point.
On a side note, right now it's printing that it didn't find the element for every time it didn't match, even if it does find it eventually. I would recommend this instead:
for (i = 0; i < max; i++)
{
if (strcmp(elementos[i], elebus) == 0)
{
printf("Elemento encontrado en %d\n,", i); //element found in
return;
}
}
printf("elemento no encontrado\n"); //not found
This is printing "elemento no encontrado" only once, and only when the string wasn't found.
I have been working on a project and I'm almost finish the last problem I have is creating a while loop that keeps asking the user if they want to convert an expression. So far it does it once then doesn't continue asking. I know this is a simple question and I think I have the logic down but for some reason it isn't working.
Here is my main:
int main(){
string answer=" ";
string expression;
while(answer!="no"){
cout<<"Would you like to do a conversion,type yes or no:";
getline(cin,answer);
cout<<" Enter a Post Fix expression:";
getline(cin,expression);
convert(expression);
}
return 0;
}
Though not really necessary for my question here is the code above my main in case it is useful:
/*
* PLEASE DO NOT PLACE A SPACE BEFORE YOU INPUT THE FIRST OPERAND
*
*
*/
#include "stack.h"
void convert(string expression){
stack k; //Stores raw input string
stack c; //stores input string without spaces
stack s;//stores the string values
string post =" ";
string rightop="";
string leftop="";
string op ="";
int countop=0;// counts the number of operators
int countoper=0;// counts the number of operands
for (int i =0; i<=expression.length()-1;i++){
k.push(expression[i]);
if(expression[i] == '*' ||
expression[i] == '+' ||
expression[i] == '-' ||
expression[i] == '/')
{
countop++;
}
}
c.push(expression[0]);
int count=expression.length()/2;
countoper=(count-countop)+1;
if (countop==countoper){ //tells when there are too many opertors and not enough operands
cout<<"too many operators and not enough operand"<<endl;
exit(1);
}
if(countop==countoper*2){ //tells when there are too many opertors and not enough operands
cout<<"too many operands and not enough operators"<<endl;
exit(1);
}
for(int i=1;i<=expression.length()/2;i++){
c.push(expression[2*i]);
}
for(int i=0; i<2;i++){
leftop=c.top();
c.pop();
rightop=c.top();
c.pop();
op=c.top();
c.pop();
post="(" + leftop + " " + op + " " + rightop + ")";
s.push(post);
if(count<6){
cout<<s.top()<<endl;
}
}
if (count>=6){
cout<<"(";
cout<<s.top();
cout<<c.top();
s.pop();
cout<<s.top();
cout<<")";
}
}
I tried this code, which I copied from yours, and it worked fine. Which made me arrive to the conclusion that I do not understand what the problem is or what you want.
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main(){
string answer=" ";
string expression;
while(answer!="no"){
cout<<"Would you like to do a conversion,type yes or no:";
getline(cin,answer);
cout<<" Enter a Post Fix expression:";
getline(cin,expression);
// do something
}
return 0;
}
What this is doing:
1) user answers "no" to first question, then user writes something on the second input. Exits program.
2) user answers something different from "no", then user writes something. Goes back to the first question.
What is the behaviour that you want? Explain better.
I also ran your code and the while loop worked for me.
Perhaps you can begin by commenting out your convert(expression); call and debugging from there!
#include <iostream>
using namespace std;
int main(){
string answer=" ";
string expression;
while(answer!="no"){
cout<<"Would you like to do a conversion,type yes or no:";
getline(cin,answer);
cout<<" Enter a Post Fix expression:";
getline(cin,expression);
/*convert(expression);*/
}
return 0;
}
I've written a function that determines whether or not to assign default values (it assigns default values if the flag is not present, and it assigns values the user passes if the flag is present). And I'm trying to test my function with a string to see if it did give me the right numbers. I keep getting "Segmentation Fault" when I try to run the tests, it compiles, but the tests just don't work. :(
Here's my header file:
#ifndef COMMANDLINE_H
#define COMMANDLINE_H
#include "data.h"
#include <stdio.h>
struct point eye;
/* The variable listed above is a global variable */
void eye_flag(int arg_list, char *array[]);
#endif
Here's my implementation file:
#include <stdio.h>
#include "commandline.h"
#include "data.h"
#include "string.h"
/* Used global variables for struct point eye */
void eye_flag(int arg_list, char *array[])
{
eye.x = 0.0;
eye.y = 0.0;
eye.z = -14.0;
/* The values listed above for struct point eye are the default values. */
for (int i = 0; i <= arg_list; i++)
{
if (strcmp(array[i], "-eye") == 0)
{
sscanf(array[i+1], "%lf", &eye.x);
sscanf(array[i+2], "%lf", &eye.y);
sscanf(array[i+3], "%lf", &eye.z);
}
}
}
And here are my test cases:
#include "commandline.h"
#include "checkit.h"
#include <stdio.h>
void eye_tests(void)
{
char *arg_eye[6] = {"a.out", "sphere.in.txt", "-eye", "2.4", "3.5", "6.7"};
eye_flag(6, arg_eye);
checkit_double(eye.x, 2.4);
checkit_double(eye.y, 3.5);
checkit_double(eye.z, 6.7);
char *arg_eye2[2] = {"a.out", "sphere.in.txt"};
eye_flag(2, arg_eye2);
checkit_double(eye.x, 0.0);
checkit_double(eye.y, 0.0);
checkit_double(eye.z, -14.0);
}
int main()
{
eye_tests();
return 0;
}
The absolute easiest way to solve this one is run it in a debugger. You probably won't even need to learn how to step through your code or anything - just fire up, run, and read the line.
If you are on a *nix system:
Compile your code with -g flag.
Load as, e.g. gdb a.out.
Run now that it's loaded - (gdb) run.
Do whatever you need to reproduce the segfault.
bt or where should give you a stack trace - and an exact line that is causing your problem.
I'm sure enough you can solve it from there to post this as an answer; but if not, knowing the exact line will make it very much easier to research and solve.
The errors are here:
for (int i = 0; i <= arg_list; i++)
{ ///^^
if (strcmp(array[i], "-eye") == 0)
{
sscanf(array[i+1], "%lf", &eye.x);
//^^^
sscanf(array[i+2], "%lf", &eye.y);
sscanf(array[i+3], "%lf", &eye.z);
}
}
i <= arg_list is wrong since you pass in 6, array index starts from 0, the max value is 5
i+1, i+2,i+3 will give you out of bounds index when you iterate from 0 to 5.
Your loop condition is wrong. It should be i < arg_list.
Think about what happens when i == arg_list.
Here's my code. It works when I comment out the "luetut" variable.
But when I compile as follows, I get segmentation fault when the program should print the variables. What sense does this make? When I try to make a debug build, something totally weird shows up (multiple definition of this and that).
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct rakenne
{
int luku;
float liukuluku;
char* mjono;
} Rakenne;
int main(int argc, char *argv[])
{
int luetut = 0;
Rakenne palikka;
// Rakenne palikka, *palikkaosoitin;
// palikkaosoitin = &palikka;
// while(luetut < 1)
// {
printf("Anna luku:\n");
scanf("%d", &palikka.luku);
// } luetut = 0;
// while(luetut < 1)
// {
printf("Anna liukuluku:\n");
scanf("%f", &palikka.liukuluku);
// } luetut = 0;
printf("Anna merkkijono:\n");
scanf("%s", palikka.mjono);
printf("%i\t%.3f\t%s\n", palikka.luku, palikka.liukuluku, palikka.mjono);
return 0;
}
So, is my gcc compiler broken or what could be the problem?
scanf("%s", palikka.mjono);
You didn't make mjono point to anything so writing to it is of course illegal - undefined behavior. Doing something like this leads to erratic behavior: the program "works" or "fails" for no apparent reason.
So, is my gcc compiler broken or what could be the problem
It's rarely constructive to think the tools you are using are the problem.
Expanding on cnicutars answer, the fix would be to allocate some memory for palikka.mjono.
Something like this:
#define SIZE 40 // or whatever you need.
palikka.mjono = malloc( sizeof(char) * SIZE );
Then later don't forget to free that memory:
free( palikka.mjono );
Or if you know what the maximum size of your strings will be, just define your structure as:
typedef struct rakenne
{
int luku;
float liukuluku;
char mjono[SIZE];
} Rakenne;
I'm writing a function that just calculates the "complementary" strand of DNA, meaning replaces C with G, T with A, and so on.
this is what I wrote:
#include <stdio.h>
#include <string.h>
#define SIZE 70
int isLegitSequence(char sequence[]);
void getComplementaryStrand(char complementary[],char sequence[]);
int findSubSequence(char sequence[],char subsequence[]);
int findSubComplementary(char sequence[],char subcomplementary[]);
void cutSequence(char sequence[],char tocut[]);
void checkDNAList(char data[][SIZE],int rows,char sequence[]);
void main(){
char dnaSequence[SIZE];
char compDnaSequence[SIZE];
printf("Enter a DNA Strand\n");
gets(dnaSequence);
printf("%d\n",isLegitSequence(dnaSequence));
getComplementaryStrand(compDnaSequence,dnaSequence);
puts(compDnaSequence);
}
int isLegitSequence(char sequence[]){
int i=0;
while (sequence[i]){
if(sequence[i]=='A'||sequence[i]=='C'||sequence[i]=='G'||sequence[i]=='T');
else return 0;
i++;
}
return 1;
}
void getComplementaryStrand(char complementary[SIZE],char sequence[SIZE]){
int j=strlen(sequence)-1,i;
for(i=0;sequence[i];i++,j--){
if(sequence[i]=='A') sequence[j]='T';
else if(sequence[i]=='C') sequence[j]='G';
else if(sequence[i]=='G') sequence[j]='C';
else sequence[j]='A';
}
complementary[strlen(sequence)]='\0';
}
However, this is what I get when I run the program:
Enter a DNA Strand
CGCTC
1
╠╠╠╠╠
Press any key to continue . . .
This is my first time using functions, so I'm not sure what I did wrong here.
Would appreciate help, but in the scope of my understanding, namely very very basic.
You need to add a prototype of the function getComplementaryStrand at the top of of the source file where the function is called.
Add this line at the top of the source file:
void getComplementaryStrand(char complementary[SIZE],char sequence[SIZE]);
EDIT: the question has changed in the meantime... Before it was a compilation error. OP please ask a new question instead of editing your original question with a new question.
Take a careful look at your for loop within your getComplementaryStrand() function. Are you assigning values to the correct string? I think not.
In getComplementaryStrand() you never fill anything in complementary string except end character. So you get garbage.