short function in c - don't understand what's wrong - c

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.

Related

Codeblocks C Debugging

I've got a new challenge to return the factorial of a number. Got ideas on how to do this, but the challenger has given some starting code - which is shown below.
Now this isn't how I would have started it (with my extremely limited experience!) - BUT I wasn't sure how system would grab some text & place within an int array - hence I tried running it within codeblocks, debugging and looking at the watch table. However I can't see 'num'.
So I tried copying num to num1:
int num1[30] = {0};
memset(num1[0],num[0], sizeof(num));
that doesn't seem to affect anything...
So question really is - is there something wrong with my codeblocks config (it debugs other programs and I've tried both cygwin & MiniGW) or is there another reason for this behavious?
#include <stdio.h>
#include <string.h>
void FirstFactorial(int num[]) {
// code goes here
printf("%d", num);
}
int main(void) {
// keep this function call here
FirstFactorial(gets(stdin));
return 0;
}

compare string with type of variables

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.

Assign a value to external variable from function

I've been reading here a lot but never posted until now.
My problem is that I'm stuck with some code. What I'm trying to do is receive a value through UART from Matlab and assign to a single variable that is gonna stick through the entire program.
This is the test code I'm running:
void start_comm(){
//Stuck in loop untill Matlab gives signal
// Spams character 'A' while waiting
while (!uart_is_rx_ready (CONF_UART)){
printf("%c\n",'A');
delay_ms(100);
}
// Start reading data sent from Matlab
// P,I,D & samplingstime data
uint8_t p_char, i_char, d_char, samp_char1, samp_char2;
while (!uart_is_rx_ready (CONF_UART)){};
uart_read(CONF_UART, &p_char);
// Print out everything out again for testing
printf("%c\n", p_char);
}
This code works, everything prints out fine. What I need is to be able to use the value in p_char in other functions and I need it to be the same value as the one sent from Matlab i.e. if it's 5 then I could printf in another function and it would print a 5.
I've tried return p_char to a different variable but it would just revert to 0 at the start of the loop. I've also tried the following test code where I try to set the variable as static:
**file1.h**
extern int a;
**file1.c**
#include file1.h
void function(){
static int a;
scanf("%i", &a);
}
**main.c**
#include file1.h
int main() {
function();
while(1){
printf("%i", a);
}
}
Looking over the code, I'm pretty sure I'm doing something wrong with the static and extern, but I'm lost.
EDIT: Figured out the problem, it was indeed Matlab code. I needed to add a delay to it to account for the time it took to communicate with the microcontroller.
Update your file1.c to read:
#include file1.h
int a;
void function(){
scanf("%i", &a);
}
This puts a in the global scope. If you keep extern int a in your .h file, C files that include that header will know about it.

Creating array of strings works in source code doesn't work in executable

I've got some code which generates an array of strings of different file names and then
passes them into a function to write some data to them. It adds a incrementing number to the starting filename which is supplied from an input argument.
The problem is that it works fine running from source in Visual Studio 2012 but when I compile it and run it as an .exe the program crashes.
The .exe doesn't appear to be passing the array of strings properly which is causing an error when it attempts to use the string
for opening a file etc.
Here is the isolated bit of code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#include <stdint.h>
#include <Windows.h>
void processing_function(int num_output, char **outnames)
{
/* in Visual Studio this works fine and prints all
the names correctly. Running from .exe will crash */
for(int idx = 0; idx <num_output;idx++)
{
printf("outnames[%d] is %s\n",idx,outnames[idx]);
}
}
int main(int argc, char *argv[])
{
/*nframes comes from another function, outname comes from input arguement */
int num_output = ceil(((double)*nframes / 1100));
int outname_len = strlen(outname)+1;
char *out_right;
out_right = (char*) malloc(sizeof(char)*outname_len);
/*Split string to append numbers before file extension */
strcpy(out_right,outname);
strrev(out_right);
strtok(out_right,".");
strcat(out_right,".");
strrev(out_right);
int out_right_len = strlen(out_right);
strtok(outname,".");
strcat(outname,"-");
int out_origlen = strlen(outname);
int num_len = 1;
char **outnames;
char *num;
char *outname_tmp;
outnames = (char**) malloc(sizeof(char)*(num_output));
int out_len;
double dbl_idx;
int *numfs = (int*)malloc(sizeof(int)*num_output);
for(int idx = 1;idx <num_output+1;idx++)
{
/*convert output number to string and stitch complete name back together and place into array */
num_len = ceil(log10((double)idx+0.1));
num = (char*) malloc(sizeof(char)*(num_len+1));
outname_tmp = (char*) malloc(sizeof(char)*(out_origlen+num_len+out_right_len+1));
strcpy(outname_tmp,outname);
sprintf(num,"%d",idx);
strcat(outname_tmp,num);
free(num);
strcat(outname_tmp,out_right);
outnames[idx-1] = (char*) malloc(sizeof(char)*(out_origlen+num_len+out_right_len+1));
strcpy(outnames[idx-1],outname_tmp);
free(outname_tmp);
printf("%s\n",outnames[idx-1]);
}
free(out_right);
processing_function(num_ouput, outnames)
return(0);
}
EDIT: Changed num_input to num_output as they do have the same value.
Running from .exe will sometimes start printing some of the names and then crash, opening the
debugger gives an error within output.c, with an access reading violation. I tried putting this code at
the top of the processing_function but that gave further problems downstream (heap corruption), which makes me think that the
code is messing up the memory but I can't see whats wrong with it, nor why it would work in VS but not as a .exe.
I could try and dodge the issue by generating the next output name on the fly every time it requires one but I'd really rather know why this isn't working.
Any help would be greatly appreciated.
I am going to take a shot and say, you passed num_input to processing_function() with outnames, outnames was allocated with num_output for size, but num_input and num_output have different values at runtime. So that lets processing_function() access out of bounds.

Console application crashes for unknown reason

I have my piece of code which is shown below
#include<cstdlib>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a;
printf("Please select a choice \n1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist");
scanf("%d,&a");
if(a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}
On running the code, it displays the menu and if i input 1, it takes a few seconds before it crashes and displays the info
document1.exe has stopped working
How can I debug this problem. I am using dev c++ 4.9.9.2
Your scanf statement is wrong. You are not passing argument (pointer) to it.
Change
scanf("%d,&a");
to
scanf("%d",&a);
For a C solution - I am not certain
aside from previously mentioned
error in scanf formatting (which your compiler should have warned about),
which would have put the result.. well, god knows where on stack…
I'm not certain, but I suspect the compiled code would take the next location in ram as an address, and write there.
Borked stack == really, really, really hard to debug errors.
Since you are using a c++ compiler, if you can use c++, you may wish to consider evaluation of using some of the STL here.
I assume you will be terminating input with a CR
int main (int argv, char** argv]
{
int a;
std::string inputString;
std::cout <<"Please select a choice \n""1.Enter New Artist\n2.List All Artist information\n3. Show sorted List of Artists\n4.Add Album to existing Artist\n5.Remove Album from existing Artist\n6.Update Artist info\n7.Search for Artist";
std::getline(std::cin,inputString);
std::stringstream inputStream(inputString);
inputStream >> a; // could also have been parsed with std::stol, or strtol. - my preference due to error checking - what if your user entered 'byte me'?
if (a==1)
{
printf("no");
}
system("PAUSE");
return EXIT_SUCCESS;
}

Resources