You're all off the mark. The answer is "Whatever you want the difference to be." ;)
Adak 419 Nearly a Posting Virtuoso
Ancient Dragon commented: Project Euler excellent suggestion +17
You're all off the mark. The answer is "Whatever you want the difference to be." ;)
This is an OOP based program, and C has no OOP support. So, you need to post this in either a Java, or maybe even a C++ forum. Definitely, not here.
fflush() on any input stream, like stdin, is deprecated, so it may very well not work at all. Use it on output streams only. ("Flush the toilet, not the kitchen faucet.")
To pull the remaining newline char off the input stream, you can use:
getchar();
right after the scanf() line of code.
Since you know that a newline will always be there, the program will not pause here. It just grabs the newline char, and goes on.
If you just want a programming problem in C, this forum is absolutely FULL of them.
Counting the words in a paragraph of text, finding a word in a sample of text, encrypting text with ROT-13, (or something stronger), efficiently finding all the prime numbers from 1 to 1,000,000, compare a binary search with a linear search for two different sizes of int arrays: 1000 numbers for 1 array, and 100,000 numbers for the other. What's the total time for finding 100 numbers? Draw a tire on the console, complete with sidewall, using ascii-like char's only. Make a Hangman game, or Tic-Tac-Toe. Draw a circuit roughly with ascii-like char's, and let the user input the resistance he needs, in total. Using just three resistors, calculate and show what the band colors and values should be for each resistor.
Go to Project Euler and tackle one of their problems.
First thing - are you getting any warnings from your compiler? And do you have your warning level turned up high?
Second, try checking the return addresses that malloc is giving your pointers. Are any of them null (indicating a serious problem)?
Third, cut down on the space where the error could be. Move your "I am here", up by 5 lines of code, until it becomes visible - now you know the error is between that line of code and the 5 lines of code further down.
Good hunting.
Sorry, little bee, but we won't do your homework for you.
This is a forum that tries to answer questions and problems relating to C.
I'm sure your grade will be reviewed, and reflect the amount of work you put into it.
You might try browsing through the "Computer Science" sub forum for topics you can use. Other than that, Google is your friend.
Good luck!
You're trying to shove a first and a last name, into the same variable, name[]. That won't work.
You can fix it easily a few different ways:
char lastname[30];
char firstname[30];
and then scanf() for the lastname, and for the firstname[] (or lname and fname, whatever).
Or you can:
char name[30]; //30 is a little small for a two name size, try 50 or 60
//fgets() is safer and a more robust input function. It will not overflow.
fgets(name, sizeof(name), stdin); //get both names as one input
name[strlen(name)]='\0'; //overwrite the newline char that fgets() adds to the name
Since all your numerical variables are doubles, I'm puzzled why you cast a few of them to float, in the program. No need to do that.
main() in C and C++, ALWAYS should return an int:
int main(void) //is one correct way to call main
{
//your program in here
//then
return 0; //indicating a normal program completion
}
Give that a try, and good luck with your classes. ;)
b, c, and d are yes.
e) is no, because a backup program will be able to make incremental backups, and save a LOT of disk speace, on the portable drive. Consider also that some files may not need to be backed up at all, because they haven't changed since the last backup.
First, decide on the appropriate data type for each input and output item. Each of them will need to be a variable (a few might be removed later, but for now, plan on one variable per item, for input and for output. All variables should be local to main(), or to the functions you call from main(). Use no global variables.
If you have to do something like sort the employees by name or pay received, then you'll definitely want to create a struct for these variables, and make the variables into struct members. (records, with fields), and then make an array of these structs.
If not, you can do this using separate variables, and working with just one employee at a time, thus using the same variables for each employee, one after the other. In either case, you first need to decide on the data type for your variables, and what name is easy to associate with what data it holds.
Names are char arrays[], and usually require 30 chars for the surname, and 30 chars for the given (first) name. It's best if the names are separate: lname[30] and fname[30]. With a NULL termination, these chars become C strings, and can be printed with the %s format specification, by printf("%s\n", lname); would print out the lname string and a newline.
Get that together and post up your code, as far as you can get it, and tell us what has you stumped at that …
C has octal, decimal, and hexadecimal converters, already built into it. Do you want to use them?
You can put 123 into a char array (a string). Then, to read it in as a decimal, sscanf(%d, &intVariableName);
To read it in as an octal, use %o, as a hexadecimal, use %x.
If you want to make a converter yourself, just post your logic (pseudo code), because I'm not understanding the logic, from the code you posted. That is, it's not clear to me, HOW you want to make the conversion, logically.
#include <stdio.h>
#include <conio.h>
int octaltodecimal(int x);
void main ()
{
int a,decimal;
printf("Enter an integer = ");
scanf("%d", &a);
decimal=octaltodecimal(a);
printf("%d",decimal);
getch();
}
int octaltodecimal(int x)
{
int base8,m,y,z,n,lastDigit,dec;
//Your while loop should start up here.
//The last digit (right hand side or 1's column), doesn't need %10, since it is octal, not decimal, and already is in the
//right format (a one's column number less than 10)
lastDigit=x%10;
n=0;
y=x; //x should be x/10, here since the right most digit is already handled
if(x>10) //x can't be greater than 10, the number base is 10.
{
z=y%10; //don't need z
while (y != z) //loop can start higher up, and remove some of the duplicated code. while x > 0 is all you need
{
y=y/10;
z=y%10;
base8=z*8^n; See pow() in math.h, for exponentiation. ^ is not what you want in C.
n=n+1;
}
}
dec=base8+lastDigit;
return dec;
}
At this point, I don't believe I can help you. Good luck.
What you have, is a mashup of data types - it's not just hex, and it's not ASCII either, but a bit of both.
That puts you WAY out in left field. If it were me, I'd take the data in, however you want, and then convert it into what I really wanted - AS A STANDARD DATA TYPE.
Probably use fgets() to get the string into a buffer, and then "coerce it" into the standard type I wanted, then use sscanf() to "lift the data, out of the buffer, and put it into a file, probably. Newlines and end of string chars, would be lost in the process.
I would definitely NOT leave the data in this mashup of two types of data, that it now is set in. There are many perfectly fine and standard data types in C. As you see here, there's a big advantage to using one of them, and a rookie mistake not to, imo.
I actually did help someone with this, a long time ago - multiple 2D X-Ray type images that all needed a bit more contrast.
Key thing was that the guy who wanted help knew the image format EXACTLY. You have to have that for every type of image you will be working with.
If you need to learn the image format, I suggest one of these:
1) Do a Google search for <name of format or extension> format. Lots of formats are kept on the net.
2) Get a really simple image file, and open it with a free hex editor. It may look like goobletygoop at first, but in time, you can start to make sense of what the file format is, from it.
Mostly, I'd work with #1 way, of course.
Your image array may need unsigned char's, signed char's, unsigned int's, signed int's, etc., so be sure to get that data type info that you will need, as well. You'll also want to know how all the characteristics of light, are handled in the image: colors, intensity, etc.
I can't tell you more, because I just did it that one time, and the poster requesting help, knew all the format info already.
Good luck!
In Pelles C it's under the Source pull down menu, then select "view line numbers".
Check around your IDE editor options, also. Not every IDE editor has this, for an option.
These "snippets" are not checked by anyone beyond a cursory look. They're typically coded by students, and shouldn't be taken as examples of great functions, in detail. Indeed, some have been dreadful, and I've shown it in detail.
In this case, you can tell it's less than elegant, simply because it repeats lines of code. It's less than efficient, because it repeats the comparison to target twice, in every loop.
It's alright if the pointers run off the boundary of the array, (or the index does), as long as the array is not referenced at that time (or the pointer isn't dereferenced). The idea it seems, is that the while loop test will fail, before that illegal comparison will be made.
I would not use any of these snippets, without thoroughly checking it out and testing them. This one is obviously, poor code. It does get the general idea across. That has some merit. I can't give it high marks otherwise, however.
I don't see anything obvious, but I'm not familiar with this, either.
If I were starting to debug it, I'd probably start with assertions to check that all the indices are staying within their proper ranges. Then start checking the subtotals, and whether j=0 is correct, for it's initialization (or should it be j=i?).
Should newmatrix be set to order-1?, etc.
There are lots of examples of this kind of code, but I don't know if it uses the cofactor method or not.
Take in the telephone number as a string.
Scan over the chars, and remove any hyphens or /'s.
If any char in the string is unwanted, then
for(each char from that point, to the (end -1) of the string) {
assign i+1 to i, in the array
}
and you're dancing on the ceiling. ;)
Not appending, but prepending!
0123 is an octal value, has a prepended 0
0x123 is a hexadecimal number, has a prepended 0x (or 0X).
0x123U is a hex number that is unsigned
0x123L is a hex integer that is a long.
0x123UL is a hex number that is unsigned and a long
The zero x prepends each hex NUMBER, but not each hex digit in the number, of course.
The original hex values, were good, but this isn't hex:
ffffffffffff0015c5f62528080600010800060400010015c5f625280a0000020000000000000a00001e000000000000000000000000000000000000
Hex would start with 0x.
Is this an absolutely HUGE hex number being represented, but lacking it's 0x?
If so, you can handle it as plain ascii, and prepend the required start to it, when you are ready to send it to your output. The rest of the time, it's just a string of char's, and you don't need hex anything for it.
So now, I believe that is the answer - leave it as ascii char's, and be careful how you work with it. For instance, input with fgets() will add or include, both a newline char, and an end of string char, to the end of the data. If you use fgets(), you'll want to remove those last two char's before the data is finally ready to use.
Do a little test with it, because I'm not sure if default chars will suffice for this. You may need unsigned chars, instead. But not hex ANYTHING. ;)
Zia, welcome to the forum! ;)
First thing you have to know is to ALWAYS highlight your code and click on the [ code] icon in the editor window. (it won't have a space before the c).
Otherwise, your code will all be squished over to the far left, and be VERY hard to study. Thus, it gets generally ignored.
Have you tried changing buff to data type unsigned int, and just reading the hex data in, as hex numbers?
It appears it would work.
This is a first for me, but how about:
1) use fgets() to read in the entire line of text, as a string.
2) Now go through the buffer, and add the extra byte (or two) that an unsigned int needs, (which sizeof(int) will tell you. So you add sizeof(int) - the sizeof(char), [which is almost always 1 byte]), and recreate the hex unsigned int numbers, in the buffer.
3) Then use sscanf() with %x, to get each of the now rejuvenated hex numbers from the buffer, into your variable for output to a file, or to a port.
Isn't it possible for the program that is producing the hex text, to output the data as hex unsigned int's, with %x? That would surely be a big convenience.
Is it possible that the "text" just looks like chars, and is in fact, unsigned int's, already? You couldn't tell just by looking, but you could tell by using scanf() or sscanf(), etc.
Change your hex array from char to unsigned integer type. Then print out your numbers, using %x (for lower case letters), or %X (for Upper Case letters) for your hex output.
I'd get this working just the way you want it, to a file, before working it into packets to send.
OK, this is the part of the help where YOU help US understand WTF you want. We weren't in the classroom, or heard the lecture, or read the text, or even know the teacher.
First - find out what you want to do. Your info here just isn't enough to start talking about a C program.
Second - Get started on it. You need more than idea's, you need pseudo code and at least the bare skeleton of a C program. Put some work into it, and THEN post it, and ask your specific questions.
You'll need to do SOME kind of looping. You don't have to use standard C loops, but some looping logic (perhaps by a call and return of a function), will be needed.
I'd suggest the game have simple rounds if you need to have 10 of them.
If you have your data in the format you want, then don't convert it any further - C'mon! ;)
You can read your hex data in as a string using
fgets(buffer, sizeof(buffer), filePointer);
Make sure that buffer has at least two char's more than the longest string of hex. One more char for the newline fgets will add, and one char more for the end of string char, also.
If you want to write out the data, don't write out the string - that has the two extra chars in it still. Instead, if your strlen(buffer) was 70. You will want to write out 69 chars only, (strlen() doesn't count the end of string char), so use a loop:
for(i=0;i<69;i++) {
printf("%c", buffer[i]);
}
You can also truncate output directly with
printf("%69s", buffer);
iirc.
Thanks, Narue! I wasn't even thinking about the possibility that Tubby meant to initialize the char array with the very low end of the character set (card suit icons and all that).
It's too late to edit my post, but Tubby, this is you warning - disregard my post above.
<Sorry, Zombie thread>
Tubby, you have three digits in your array. It's not a string, because it has no end of string char at the end of it, and it's not three numbers, because the type of the array is declared to be char.
It's just the digit '1', and then the digit '2', and then the digit '3'.
And that's all it is, right now. Without quote marks in there, it may not compile, as is.
LevyDee is right IF the array had been sent to another function. In the function it was declared in, it will have size 10.
Tubby, the union has ONLY a size big enough for the largest member. THE largest member, not the SUM of all the members. That would be true for a struct, though. (along with some padding which would depend on your compiler, as LevyDee mentioned).
userinput[2] == userinput[2] ??
C'mon! ;)
When you have a complex line of code, break it up into multiple lines of code, so each line of code is easier to understand, and debug.
In C, the name of the array, is ALMOST the same thing, as a constant pointer - and it points to the first element of the array - array[0].
So
char a[] = "Hello there.";
now the address of a, is the address of a[0], and a string operation includes all the char's until the end of string char is reached: '\0' or 0.
After the last e in a[] above, there would be an end of string char to mark it, even though you can't see it when the string is printed.
suppose i want to read excel contents of 123 as 'one hundred twenty three'. please provide suggestion. srinivas
First, you have to know how to read, and what an excel file is. It helps greatly if the excel file is with arm's length of your eyes
Second, you need to understand that we require you to put forth some real effort FIRST, and ask specific questions about what has you stumped. We won't post up code, before you do, nearly always.
Third, you gotta know you need to store the words which match up with the numbers, in a two dimension array of char's:
*numbers[]= {"zero", "one", "two", "three", etc.}
Now what happens when you printf("%s\n", numbers[0]), or numbers[1], etc.?
And welcome to the forum! Srinivas ;)
How about a new line 11 1/2:
if(expression=='q')
break;
You should have a getchar() in your code, (maybe line 11 3/4ths, to remove the newline that scanf() always leaves behind.
Line 39:
for(int j=0;j<=pos;j++)
buffer[j] = '\0';
Line 39 and 1/2: ;)
pos = 0;
I would learn the basics of the C language first. Can't do anything without that. There are lots of C tutorials on the web, including one's for this forum, and other programming forums with a C section on them.
If you just want to manipulate images, I'd use software designed just for that, (GIMP, Photoshop, etc.)
If you want to learn to do it in C, then I'd see what answers you get here, but also, Google it, because this is a specialty application. As I understand it, you need to get a library that works with your format of image files, after you learn the basics of C, and then work through an intro on using that library, and work your way up, from there.
I doubt if you'd want to do it all "from scratch", but that's always an option with C, and just my opinion.
Good luck!
That's an acronym, WaltP. DNA, RNA, Nasa, etc., are all acronyms.
An anagram has to have every letter of the word or phrase it can be rearranged to form.
A simple game might have pseudo code for a game loop, similar to this:
int main(void) {
char answer='y'
while(answer == 'y') {
prompting Do you want to play a game of ... ? [y/n]:
scanf("%c", &answer); //get user's answer
getchar(); //remove left over newline char from input buffer
if answer == 'y'
call play1game function
}
print Goodbye
return 0;
}
This is just to show some logic. It's not a program.
Good, consistent formatting, may not seem important yet, but over time, your eye/brain, will become trained to recognize many standard C idioms, and EASILY catch many errors in both syntax and logic. The more you program, the better your "eye" will become.
Exactly right - rta is not a word or phrase.
After EVERY scanf("%c...), when you are storing a char data type, add this:
getchar();
Right after it. OK, the last one in the whole program, you don't need to add this line of code.
What it does is remove the newline char that scanf() always leaves behind in the input stream (unless you use a special format).
This is THE most common problem for new students, in C. (Until they learn pointers) ;)
If you just want to get rid of the newline char, this works great. Put it as the next line after fgets():
int len=strlen(char_buffer);
if(char_buffer[len-1]=='\n')
char_buffer[len-1]='\0';
Your string will look like this after fgets():
my string\n\0
and what this makes the string into is just:
my string\0
Because it seems like white space wasn't the problem, but the newline char was.
You should post in the correct forum, and use code tags around your code. Hard to study the code unless you use code tags.
Delete line 8, put lines 4 and 5, where line 8 is now. Right after the scanf() line, add this:
getchar();
//new line 5:
difficulty = '0';
And you don't need to assign diff in two places. Just assign it once, just before you return from the difficulty function.
I'm not sure what the whole "weak" "pro", and "low", "high" print out to the user is for. Either use one, or the other, but both seems quite redundant.
If you indent your subordinate code lines, your code will be easier to troubleshoot, by far.
One more thing - your choices are just two, difficulty and diff, being either l or h. So just
if(diff=='l') {
//do something for low diff here
}else {
//do something for high diff here
}
fflush() on any input stream, is undefined. It may work on some compilers and operating systems, but is not standard, and you'd be wise to use fflush() ONLY on output streams, not input streams.
Think of it like the plumbing in your house. You can flush the toilet, but flushing the kitchen sink is just goofy, and not likely to work.
One way to handle the extra newline that scanf() always leaves behind, is to put this:
getchar();
right after the scanf(). That will pull off ONE newline char, from the input buffer.
Always good to have this, whenever you are using scanf() to obtain a char. It probably will not bother a scanf() for integers, floats, or strings.
This:
while((ch=getchar()) != '\n');
will remove all the char's in the input stream up to and including the first newline char. Which is normally all you'd ever want, at any time.
A good example of that is this:
#include <stdio.h>
int main(void) {
int num;
do {
scanf("%d", &num);
}while(num != 1);
return 0;
}
But enter a letter, instead of a number, into the above, and watch it go nuts!
Now try the same loop with a getchar added:
#include <stdio.h>
int main(void) {
int num;
do {
scanf("%d", &num);
getchar();
}while(num != 1);
return 0;
}
And see how much better the loop behaves, now! Pretty amazing, imo.
There are scanf() formats that can also handle this newline …
Game programs use a BIG loop to display the menu choices. They have calls to various functions which depend on the user's choice, and they will return to main (and end the program), when the user chooses the quit exit, or end choice.
Usually, it's easiest to use a simple while(1) or for(;;) loop, and then near the bottom of the loop, test if the user has requested to end the program. If so, the program will break out of the loop. (with a break statement)
Note that if the user wants to quit a single game, that choice will be made in another function, completely. (In yes, another big while or for loop.)
So you have a menu() which calls a play1Game() function, which in turn, will call other functions to actually calculate the right move, make the move, and etc., for that one game. Then the program will return to the menu() function to see whether the user wants to play another game, see game stats, or quit entirely.
Search Google for ROT13 and you'll have links. Wikipedia is usually good for info on things like this - might go there first, actually.
ROT13 is just a simple replacement scheme. Each letter of the text is shifted up by 13 letters. That doesn't work for letter in the top half of the alphabet though, since there are only 26 letters. So those letters in the upper half of the alphabet, are shifted 13 letters down, instead of up. Handling that assignment, will take one if statement, at least.
Then you have the uppercase letters, and again, A through M are shifted upward, but letters > M must be shifted downwards, and again, all shifts are by 13 letters. That will take another if statement to handle that, at minimum.
In the code you posted, it's handled all together, by using if else if, else if, and else if, in one block of code.
The reason it works out so easily, is simply because 13 is half of 26. Needless to say, it's not a secure encryption, at all.
You did not declare a prototype for your function. Default would be an int, which doesn't match what the compiler sees, when it gets down to the function.
Put your function prototypes above main(), with the right data types (the actual names of the variables are not needed), just int, char float, double, int *, char *, etc.
The "implicit declaration" is the declaration that the compiler is trying to make work, because you did not an explicit declaration of the function.
C always passes parameters by value (a copy), BUT a copy of an address (like a pointer), is REALLY still an address, isn't it?
So there's your copy by reference or address, right there.
Remember also, that an array, when being sent as a parameter to a function, will always be degraded to a simple pointer. When the function receives it, it has to be told what the type of pointer actually is (array with 1 dimension, or array with two dimension, or whatever).
Array passing to a function, especially arrays with 2 dimensions or more, take some extra study and practice.
Visual Express, Code::Blocks, and my current favorite (for Windows only), Pelles C.
There is a definite learning curve when you move to any new IDE, but overall, they increase your productivity quite a bit for C, in my experience.
I'm shocked that anyone would suggest using a command line (terminal) interface alone, instead of an IDE. OK scratch that, I'm **VERY** shocked that anyone would suggest using a command line interface for programming.
Aia, do you really believe that a CLI is more productive than using an IDE? I generally like your answers, but this one is off the mark.
If by "word file" you mean a text file, just use fopen() with the name of the file (surrounded by double quotes), and the file access mode of "w", and after you've written out your text, fclose(filePointerName), the file.
If you mean export your C text file, in Word (the editor Word), format, that can't be done at all easily, because that format is proprietary, and changes from time to time. If this is what you want however, the way to do it is to get your C program's output as plain text, and then use Word to open the file as a text file. Now save it as a Word file document, and add any extra formatting that you want.
If you have the compressed version, uncompress it (7zip works well and is free from the download.com site and others).
Then read and follow the directions in the readme file (I can't recall if it's in TC or TC/BIN, but one or the other).
You may need to add TC and/or TC/BIN, to the path, I know I did, but don't recall if it was absolutely necessary or not. This is a forum, so save your email addy.