Assign a value to a Character String..? - c

Here's my problem.. I have two arrays: one has the total marks of students and another is a character string. So, I want the computer to assign a letter grade to the character string when the array with the total marks is in within a range..
Here's what a came up with but it won't compile and I don't have anything else in mind:

Try this:
sprintf(grade[i], "%s", "A+");

What is the compilation error message?
I'll assume you've allocated resources properly. Even if you have and everything is compiling this line of code will cause an array bounds bug (as you've defined things):
grade[i][MAX_LETTER_SIZE]
should be:
sprintf(grade[i], "A+");
I really would send arrays as pointers. It is much cleaner and more efficient. You might also wish to create an LUT that maps scores to grades. This will reduce the amount of code you need to write. Otherwise you'll require a series of if statements rather than an inner loop with a single if statement. Good luck.

Related

'If' Condition Not Working As Expected In My C Code

I am fully aware that this is due to some error overlooked by me while writing my text-based calculator project in C, but I have only started learning C less than a week ago, so please help me out!
Since the entire code is 119 lines, I'll just post the necessary snippet where the real issue lies: (There are no errors during compiling, so there is no error beyond these lines)
char choice[15];
printf("Type 'CALCULATE' or 'FACTORISE' or 'AVERAGE' to choose function :\n");
gets(choice);
if (choice == "CALCULATE")
The bug is that even after perfectly entering CALCULATE or FACTORISE or AVERAGE, I still get the error message that I programmed in case of invalid input (i.e, if none of these 3 inputs are entered). It SHOULD be going on to ask me the first number I wish to operate on, as written for the CALCULATE input.
The code runs fine, no errors in VS 2013, and so I'm sure its not a syntax error but rather something stupid I've done in these few lines.
If you use == you are comparing the addresses of 2 arrays, not the contents of the arrays.
Instead, you need to do:
if (strcmp(choice, "CALCULATE") == 0)
Two things to mention here:
Never use gets() it has serious security issues and is removed from the latest standard. Use fgets() instead.
To compare strings, you should use strcmp(), not ==.
The problem is you're trying to compare a string literal with a char array. C isn't found those things being the same, since the '==' comparison operator is not implemented in that way.
You have two options for performing that comparison :
1) Use the strcmp() function, from string.h library
2) Manually comparing the chars in your array, and the string literal
Definitely, the first option is the easiest and cleanest one.

Removing a substring from a char array without using any libraries in C

I am a computer science first year student and our teachers gave a binary pattern search task to do. We have to remove a substring from a string without using any libraries and built-ins like(memmove or strstr). Our only hint is that its something to do with '\0'. I don't see how we are going to achive this because as i know the null character only ends a string not removes it. And given an unknown input it gets even harder to get around. I need help about the usage of null character. EDIT: Oh and also we are not allowed to create new arrays. EDIT2: The problem is much more complicated if you are here for the solution read the comments under this thread and the marked solution's also.
C strings are "null terminated" which means they are considered to end wherever a null (written '\0' in C) appears.
If I start with the string "Stack Overflow" and I overwrite the space with '\0', I now have the string "Stack". The storage for "Overflow" still exists, but it is not part of the string according to C functions like strlen(), printf() etc. In fact, if I hold a pointer to the "O" part of the original string, it will be just as if there are two strings: "Stack" and "Overflow", and you can still use both of them.
It's like if I come to where you live and I build a huge wall across the road just before your house. The road is now shortened, and people on my side of it won't know you are there.

Having trouble with a c program that separates strings into paragraphs. sentences and words

The program needs to get input text and an option from the user. At this stage I am currently working on the ap: option which reads a text from the user and appends the corresponding data to the corresponding arrays. (Also I have to input the text with the option as a prefix, e.g. ap:Text, because that is how the bot that tests my program inputs it.).
Here is the code: https://gist.github.com/Kritsos/03d08f29beb97d24eba1cbc4e83962ab
I know it's a bit difficult to follow it so I will try to explain it as better as I can. First of all I know that there are a lot of memory leaks, I will tend to them as soon as I get the 3 functions working. I have tried to dynamically allocate almost everything (that's why I use triple pointers) and I hope that the allocation is correct and not the cause of the problems. The par function is supposed to get a paragraph from the input text which is easy because the input texts is a paragraph on its own so I just copy the text to the paragraph array. Now the sent and word functions are the ones I am having trouble with. Both are based on the same logic. I take the input text from the user, I try to find the ending character( ".!?;" for sentences " " for words), place a '\0' there, copy the string to the corresponding array then do the same thing but instead of checking the whole input text again I start from the position I placed the '\0' + 1.
Your code has numerous errors in it, the critical one that's causing your current crash are lines 8, 32, and 59. All variations on this:
**paragraphs = realloc(**paragraphs, *num_par * sizeof(char *));
You're calling realloc on pointers that were never allocated in the first place.
The lesson here isn't "be careful with pointers" or anything like that, though obviously you should be. The lesson is that tons of indirection becomes difficult to reason about. Rather than try to make a three-star solution work, you should explore a different approach that doesn't require these kinds of code gymnastics.

Difficulty in reading in a DNA sequence file using C code

I am currently taking Coursera Bioinformatics course, and doing the programming assignments. Being, a first year undergraduate just learning C, while I am aware that python is the language that's becoming much more popular for bioinformatics, I am challenging myself to implement every single algorithm in the course in the C language to master it, as it will also benefit me in all my CS courses here, which use C/C++ a lot.
In working on one of the assignments, our goal is to write a program that can take in a shorter DNA pattern and compare it with a long complete DNA strand, and the output the count, which is the number of times the shorter DNA pattern appears in the long complete DNA strand. We are given a file consisting of all the inputs we need, and the gold output, but I am having great trouble in parsing the file properly, even though I've consulted my textbook and numerous documentation. I actually have no problem implementing the algorithm itself; I tested it using smaller hardcoded character arrays in the program itself.
The input file is as follows:
Input
TAACAGCCTTTAGCCTTTAGCCTTTAGCCTTTAGCCTTTAGCCTTTGAGCCTTTGAGCCTTTTAGCCTTTCAGCCTTTAGCCTTTAAGCCTTTCCGCATCGAGCCTTTCAGCCTTTCGTAGCCTTTCGAGCCTTTAGCCTTTCAGCCTTTAGAGCCTTTAAGCCTTTAGTCGATGTAGCCTTTAGCCTTTAGCCTTTAGCCTTTGCAGCCTTTAGTAGGCAAGCCTTTTAGCCTTTGAGCCTTTCGAGCCTTTCTCGCTAGCCTTTAGCCTTTGGTGAGCCTTTTAGCCTTTAGCCTTTTCGCAGCCTTTTGAGCCTTTCTTGTTTGAATGGCAAGAGCCTTTTCGAGCCTTTAGCCTTTGAGCCTTTCAGCCTTTAAAAGCCTTTCGTTAGCCTTTAGCCTTTATCGAGCCTTTAAGCCTTTTATGCAAAGCCTTTGAGCCTTTAGCCTTTCAGCCTTTCAGCCTTTCATTGACAAGCCTTTCAGCCTTTAGCCTTTAGCCTTTCTCAGCCTTTGAGCCTTTGAGCCTTTGTCGAGCCTTTTTTCAGAGCCTTTTAGCCTTTAGCCTTTGAGCCTTTAGCCTTTAGCCTTTTACGAGCCTTTGCAAGCCTTTCAGCCTTTCCAAGCCTTTAGCCTTTGCTTTAGCCTTTCATGGGATAGCCTTTAGCCTTTATTAAGCCTTTTTTATCAAGCCTTTGTAGCCTTTAAGCC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AGCCTTTGTTGCAGCCTTTAGCCTTTAGCCTTTTAGCCTTTAGCCTTTAGCCTTTGAGCCTTTGAGCCTTTTAGCCTTTAGCCTTTGAGCCTTTTGGACAGCCTTTCTGAGCCTTTCGTAGCCTTTACCGCAAGCCTTTATAGCCTTTGAAGAGGAGCCTTTATAGCCTTTCAGAAGCCTTTTAAGCCTTTTCGCAGCCTTTTATCAGCCTTTAGCCTTTAGCCTTTTAGCCTTTCAGCCTTTAGCCTTTACAAGCCTTTAGCCTTTAGCCTTTATCAAGCCTTTCTAGCCTTTGAGCCTTTGTGAGCCTTTGTGTCAGCCTTTCAAGCCTTTTTAAGTACAGCCTTTACTCAGCCTTTATAGCCTTTGTCGTAAGCCTTTAGCCTTTAGCCTTTGAAAAGCCTTTACGCACAGACAAGTAGCCTTTCAGCCTTTAAGCCTTTGAGTATGTCCTTGAGCCTTTAAAAGAGCCTTTGGTAGCCTTTAGCCTTTAGCCTTTTATAGCCTTTAAGCCTTTAAGCCTTT
AGCCTTTAG
Output
294
First line is the string "Input"
Second line is entire DNA sequence (which as the label "Input" implies, is my input DNA sequence, and which I will refer to it throughout as text)
Third line the shorter DNA sequence, which I will refer to as pattern.
Fourth line is string "Output"
Fifth line is the gold output, the number of counts which my program should be returning.
I tried parsing the file with the following code:
int main(int argc, char **argv)
{
if(argc>1)
{
FILE * dataset = fopen(argv[1], "r");
if(dataset==NULL)
{
printf("File count not be opened or found!\n");
return 1;
}
char in_label[1000], dna_text[10000], dna_pattern[1000], out_label[1000];
int count=0;
fscanf(dataset, "%s, %s, %s, %s, %d", in_label, dna_text,dna_pattern, out_label,&count);
... and other code below that calls the counting algorithm which I won't show here ...
While my call to fscanf does return me in_label correctly, it does not work for the remaining arguments. Basically when I printf out each of my in_label, dna_text, dna_pattern, out_label and count, only in_label correctly gives me the string Input, but the rest all are garbage. I'm really confused, because I thought that the fscanf function automatically skips linefeeds or spaces when reading in from the stream. So why did Input get correctly read into in_label, but not the others???
Also a second question I have is about one shortcoming that I'm aware of in my program, which are the hardcoded array sizes. I know about malloc function, and just learnt about it this week in class, but I just can't figure out how to use it here. Because in order to use malloc, we need to be able to at least "soft code" the size of our array in advance, and here, I just can't imagine how I would be able to tell the compiler, in any "soft coded" manner, what my array sizes will be especially for the dna_text array, which varies greatly from dataset to dataset.
C is really challenging, a world away from python whose convenience I've been so spoilt by. I would greatly appreciate any help to overcome this issue, so that I can move on with my learning of bioinformatics. Thank you very much!
You can use fstat() to get the file size and malloc() for allocating proper buffers
Use fgets() for reading text files line by line. Do not use fscanf() at all.
Never read a file at once as you should never know what exactly went wrong if your reading API returned an error. My experience tells me that reading line-by-line is the best strategy when working with text files. Just be sure you have a buffer large enough for storing the longest possible line.

How to sort this specific string array by using the following set of rules(?):

Yes, I'm a newbie and my uncle has challenged me to use the function:
void sortStrings(char str[], const char* delim){...}
to sort the given char array str in a way that every char from delim that shows up in str will separate a group of chars in str thus making them the words you need to sort by hex value. In the process I also need to replace those word separating chars from delim with ';'.
The rules are: I may only use the library <stdio.h> and I can't use malloc/realloc.
Apparently this should be done with an O notation of n^2 (n being the amount of words in str, not chars)
and here's an example of an input and output:
input:
char str[] = "aaa*test,hello.world*abcd.zzz";
sortDelim(str, ",.*")
output: str is now: "aaa;abcd;hello;test;world;zzz"
Well, I've managed it now eventually, the bubble sort thing kinda helped so tyvm for that :)
note: I'll leave this thread here in case anyone wants to take this challenge on himself? It ain't easy I promise :P If you think I should delete it or add the finished code then just ask(Please don't deduct my rep even more ><)
You are off to a good start. After your second for loop you have
replaced all delimiters with semicolon
you know how many words there are, size
you know how many letters each word has, letters
One observation is that you have allocated letters to be 1000 entries. That seems like enough, but is it really? How do you know how many words there are in str? You don't. And you can't use malloc to allocate dynamically, so perhaps you need to look for an algorithm that doesn't require that lookup table? You need an algorithm that works in place
http://en.wikipedia.org/wiki/In-place_algorithm
What's next? You need to sort the words. There are many sorting algorithms. You want something simple, and are allowed complexity O(n^2). Here's a list of sorting algorithms:
http://en.wikipedia.org/wiki/Sorting_algorithm
Notice that in the table, under 'other notes' it tells you that some algorithms are "Tiny code size", that sounds good. Sort the table first by 'other notes' then by 'Average' complexity (click on the triangle in the column header). You now have two algorithms that use a method 'exchanging' (that means in place), with Tiny code size and average complexity O(n^2), these wikipedia links explain how they work, and include pseudocode to get you started:
Bubble sort
Gnome sort
Take your pick and try your hand at it.
Hint: you probably want a subroutine that exchanges (swaps) two consecutive words if the first word compares greater than the second word. This can be done in place.
Suppose you have
abcd;aaa
The first word is greater than the second word, you need to detect that, and swap the words so you end up with
aaa;abcd
Here's a diagram that will give you the general idea.

Resources