I'm taking a basic programming class and we can do practice programs as we go to better ourselves. Right now, I'm trying to write a program that uses a while loop to ask for inputs from a user then the program decides if those inputs meets a certain value or not. The trouble I'm having with is the output.
For example, "You have met this certain number X times out of Y times."
What do you use to keep track of how many inputs was put in and how many met the criteria?
Typically you'd use a pair of int variables for that. Initialize both to zero, then increment one each time through the loop that reads the input (so it counts how many inputs there were), and increment the other within an if statement that checks whether the input meets the criteria (so it counts how many inputs met the criteria). At the end of the loop, the two variables hold the numbers that you want to print.
im looking for a base conversion function in c that could do conversions from bases 2 up to 36, including bases with characters A-Z.
For now i just found on the web functions that deal with base 2, ten and hex and a bit limited.
For this project, it would probably help to understand how bases work. In any case, let's walk through a process for how one might convert to, say, base twelve. This should be the simplest method to implement.
First up, we have our decimal number, since that's an easy place to start. Let's say, I dunno, 1452 is our number. We'll also need an array of characters for what each character is, since that'll be a lot easier than a straight ASCII conversion, where the number characters and letter characters are separated.
int dec=1452;
int toBase=12;
char outputs[36]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
Following that, we probably will only be OUTPUTTING the result in another base - it doesn't make sense to store it multiple ways, and makes your conversion process simpler by only converting from one base to any other given. We could store the result in a character array, but again, we already have the number stored - no point.
For this method I'm going to describe, we'll need a buffer variable to keep track of our number as we convert parts of it.
int buf=dec;
Next up, we'll start counting spaces back in the base we're going to, 12, and see what each space is worth. We'll continue until we pass our number, then backtrack one. We'll also need to save what space we're on for a for loop from that to the first space later.
int space=0;
while(Math.pow(toBase,space))<buf){
space++;
}//Braces added for clarity
space--;
Now, this is the main calculation loop, where we'll output the result. Again, the original number is still stored in 'dec,' so we don't need to worry about loss of data or changing it at all.
int i;
for(i=space;i>=0;i--){//We have set up the for loop to check each space as we progress
int modResult=buf%Math.pow(toBase,i);//Gets the number that goes in this space of the resulting base number
buf-=modResult*Math.pow(toBase,i);//We have that, so take it out of the number
printf("%c",outputs[modResult]);
}
Because of the way we're doing this, going from the top space to the bottom, modResult will never be higher than the highest number our base can go in. With this, your program will output to console the resulting number. Also, keep in mind that this only outputs the number - for the purposes of storage and calculation, it's much simpler to use the built-in functions that use base 10. Furthermore, be careful that your toBase variable never goes above 36.
As a further note, I numbered the digits (spaces), from right to left, starting at zero, because the far right space is 1, represented by your base to the zeroth power. Hope this helps.
I've been searching a while for this, and couldn't find an answer. I will be appreciated if someone knows how do to that!
PROBLEM: I must code a program that will store some numbers, but I don't know how much numbers there will be! What can I do?
I was wondering if I could use timing to get things done. I mean, if 5 secs has passed and there is no input of data, then start processing these numbers. It would work, but I couldn't code this. Can someone help, please?
1) first solution:
You can ask the user to enter the number of desired element at the beginning.
2) second solution:
Keep scan numbers till you get EOF from the user. and store the input number into a linked list or a dynamically allocated array (resize your array size withe the realloc)
3) third solution
keep scan numbers with a timeout. If there is no input during the timeout then the program will consider that the user have finished input numbers and then the program stop reading from stdin. The input numbers could stored into linked list or dynamic array as indicated in the second solution. Use select() with the scanf() in order to add the timeout behaviour as indicated in this answer
this question was asked in an interview..
assume your computer is reading characters one by one from a stream (you don't know the length of the stream before ending). Note that you have only one character of storage space (so you cann't save the characters you've read to a something like a strong). When you've finished reading you should return a character out of the stream with equal probability.
how to approach this problem?? any idea??
any way to slove this??
It's one of those tricks that you either know or don't:
Take the first character, with probability 1/2 take the next one, otherwise keep the first one, with probability 1/3 take the next one, otherwise keep, etc.
It works because every time you pick the n th char with probability of 1/n, or keep the previous one (that had probability 1/(n-1) to be there) with probability (1-n)/n, and the 1-n s cancel.
The question is quite simple, and I hope the answer it's simple too :)
Let's say you are given a pattern, and you have to read a text file, and every line is composed by a first digit, that indicates the number of "patterns" in the string, and the the patterns element separed by one space.
For example, given the pattern
key value
a valid line of the text file could be
3 10 "apple" 15 "orange" 17 "melon"
If the number N of repetitions is fixed, I'd use something like
fscanf(inFile,"%d %s",&n,str);
but is there a function that allows me to give the number of repetitions as a parameter, or I should scan each line and extract values I'm interested in, using substr and atoi?
The "trivial" way is obvious, I'm looking for something more "professional" and effective.
Use fscanf() in a loop: first extract the number of repetitions N, then loop N times extracting your pattern.
If you're looking for something more professional or sophisticated, you might want to move away from the standard C library and towards a regex or parsing library, or something mentioned heere: http://www.and.org/vstr/comparison. While I won't go so far as to say you can't do string processing easily or well in C, it's not a strong point of the core language.