Monday, May 24, 2010

Explain the following program in C.?

main()


{


int i=3;


float j=3.5;


printf("%f",i);


printf("%d",j);


}

Explain the following program in C.?
Using %f notation we print a float. Here i is a integer but printing using %f notation the it will print 3.00000





But %d notation is for integer printing so if we print a float of value 3.5 then it will print 3 only





%f -- float printing


%d -- decimal printing
Reply:Others have spotted the mistakes; there, but if this is just an exam question and they are making 'mistakes' purposely the traced output should be 0234327823784 or some weirdly long garbage number.
Reply:A simple program which prints two numbers - an integer (3) and a float (3.5)





1. The first line defines the program


2. The 3-rd and 4-th lines define the type of the numbers (3 is integer, 3.5 is float) and assigns them to variables - i and j


3. The "printf" command is used to print the numbers on the screen. "%f" means that Decimal floating point will be used to format the number, "%d" means Signed decimal integer.





So the output of the program will be the numbers 3 and 3.5





I see a mistake here. Wrong formatting is used for the numbers. Choose one of the following:


1.) printf("%d",i);


printf(%f",j);





-OR-





2.) float i=3.5;


int j=3;


Answer this trick question in C language...find the output of this...?

#include%26lt;string.h%26gt;


main()


{


char str1[] = "Infosys";


char str2[] = "Infosys";


if(str1==str2)


printf("\n Equal");


else


printf("\n UnEqual");


}

Answer this trick question in C language...find the output of this...?
prints "UnEqual"





the == function will actually be checking if the strings are at the same memory location. To compare strings, you need to use strcmp() and check that the return value is zero.


Please help me fix this c program---thanks?

okey...this program works properly,, all i need is to organize my results... let it display. the compound interest with the rate being __is: and so on.. please help.





#include %26lt;stdio.h%26gt;


#include %26lt;math.h%26gt;





int main(void)


{


double amount;


double principal=1000.0;


double rate= .05;


int year;


int six_percent;





printf("%4s%21s\n", "year", "Amount on deposit");





for(year=1 ;year%26lt;=10 ; year++)





for(rate=.05; rate%26lt;=0.10; rate+=.01)


{


amount=principal * pow(1.0 +rate ,year);


printf("%4d%21.2f\n"year,amount);


}


}





the program needs to display the compound interest using 5%,6%,7%,8%,9%,10% as the rate. the questions specifies that i must use a for loop..

Please help me fix this c program---thanks?
dude why dont you just not set the rate standards and have it loop right then in one of you printf statements ask the user t enter the following rates


such as 5 6 7 8 9 and 10
Reply:I don't know if it's the only problem, but you need a comma in the printf statement between the second " and the year variable.


De Morgans Law in C++?

I am having some problems trying to compile this program can anyone help me out?








#include %26lt;iostream%26gt;


using std::cin;





int main()


{





// input range to scan values


cin %26gt;%26gt; lowerx;


cin %26gt;%26gt; upperx;





// loop over all combinations of x and y


for (int x = lowerx; x %26lt; upperx; x++)


{


for (int y = lowery; y %26lt; uppery; y++)


{


if (!(x%26lt;5)%26amp;%26amp;!(y%26gt;=7) != !((x%26lt;5)||(y%26gt;=7))) // test equivalence


{


printf("Expressions are not equivalent. x = %d, y = %d", x, y);


return;


}


}


}





printf("Expressions are equivalent over the specified range");


return;

De Morgans Law in C++?
are you retarded? Do you have Suleyman? ;)





you never declared any of your upper or lower variables





you need your returns to be return(0);





and you're missing a '}' after the last return.
Reply:Good answer! Would be best, if you didn't call him retarded, though.





Use a syntax checker that will show you on which line something is wrong. Report It

Reply:Do you have "lowery" and "uppery" defined?

love song

Need help with a C Program?

#include %26lt;stdio.h%26gt;


void main(void)


{


int a,i,j;


clrscr();


for(i=5; i%26gt;=1; i--)


{


for(j=1; j%26lt;=i; j++)


printf("%d",j);


printf("\n");


}


getche();


}








I need the Output as:





12345


2345


345


45


5





Thanks in advance...need it Urgently

Need help with a C Program?
ok here the code that i came up with also do you need that clrscr function if so sorry:


#include %26lt;stdio.h%26gt;


void main(void)


{


int a=1,i,j;


for(i=5; i%26gt;=1; i--)


{





for(j = a; j%26lt;=5; j++)


{


printf("%d",j);


}


printf("\n");


a++;


}





return;


}





the reason yours kept counting down is cause everytime you went back into the second for loop the was intialaited to one so i add a and had it counted up every time the second for loop was finished and just had the condition for the j loop be that it was less then or equal to five instead of i cause eveventally it would catch up at 3 so I hope this helps without the clrscr





EDIT: to albert this is C not C++ but that would work instead of using a you could just set it equal to i and it would do the same thing with less varibales and use less resources
Reply:#include %26lt;stdio.h%26gt;


void main(void)


{





clrscr();





int x=0, y=0;





for(x=1;x%26lt;=5;x++){





for(y=x;y%26lt;=5;y++){


printf("%d",y);


}





printf("\n");


}





getch();


}








Kindly try this.
Reply:Why do not you contact a freelancer from websites like http://getafreelnacer.com/ ?
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int a,i,j;


clrscr();


for(i=1; i%26lt;=5; i++)


{


for(j=i; j%26lt;=5; j++)


{


printf("%d",j);


}


printf("\n");


}


getch();


}





this program is a litlle altered but it will work.Wat this program does is that it will print the innner loop value and the condition is that is %26lt;=5 so first run of loop it will run from 1 to 5 then scond time 'j' will be initialised to 2 and not 1 as i have said j=i so therefore it will print from 2 to 5 and so on until last time it will print only 5 this program should work in case i am wrong plz forgive me.thank you


My answer to my c language question,pls check it for me..?

this is my answer to my question that i wrote yesterday...





#include %26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





double hypotenuse(double,double);





double hypotenuse(double a,double b)


{


return sqrt((a*a)+(b*b));


}





void main()


{


double side1,side2;


int i;





for(i=0;i%26lt;=3;i++)


{


printf("Enter side 1 and side 2 for triangle %d :",i+1);





scanf("%f%f",%26amp;side1,%26amp;side2);





printf("Hypotenuse for triangle %d is %f",i+1,hypotenuse(double,double));





}





return 0;





}

My answer to my c language question,pls check it for me..?
I personally would ask for the 2 sides separately. The scanf seems to be the only possible issue. I don't know how it separates the 2 floating point numbers.


How do I use C to program a delay?

Just for example. How would I get the programme to say:





printf("blah blah");





Then delay here for a set length of time





printf("blah blah blah");





And so on..

How do I use C to program a delay?
on a unix system you would call sleep(seconds);
Reply:Why don't you join one of the hundreds of C forums or user groups where answers to your question will continue for weeks. You will learn far more from these groups than by asking a single question here. That's how I learned to program.
Reply:Here's one way. The time function is found in (at least) DOS, Unix, Win16, Win32, ANSI C, ANSI C++ and OS/2.





Your compiler may offer a better function. For instance, on a DOS, Unix, Win32 or OS/2 system, you could use sleep(), but that's not ANSI C. What I'm offering here uses up a lot of CPU cycles, so it would be OK on an individual PC, but it would not be very acceptable on a multiuser system such as a server.





#define DELAY 4


#include %26lt;time.h%26gt;


#include %26lt;stdio.h%26gt;





int main(void)


{


time_t begin, end;





end = begin = time(NULL);


printf("blah blah");


while ((end - begin) %26lt; DELAY)


{


end = time(NULL);


}


printf("blah blah blah");





return 0;


}
Reply:There is a function in C that works for delaying or sleeping for a certain amount of time....most times, this time is specified in microseconds (i think so...its been long since i used C).





delay( )


The delay( ) function found in dos.h is processor dependent. And it won't work on all


systems. The reason is the delay function is implemented with clock speed.





Simply try writting "delay(400);" It might just work.





Dont forget to include "dos.h", thats the header file for delay function.
Reply:#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;dos.h%26gt;





main()


{


printf("blah blah");


delay(1500);


printf("blah blah blah");


getch();


}


I tried to run this pgm in both turbo c&dev,i am getting error in turbo,but in dev it will compile,no res y?

#include%26lt;stdio.h%26gt;





main()


{


int radius;


float area,perimeter;





printf("\n Enter the radius of a circle");


scanf("%d",%26amp;radius);


areaperi(radius,%26amp;area,%26amp;perimeter);





printf("Area=%f",area);


printf("\n Perimeter=%f",perimeter);


}





areaperi(int r, float *a, float *p)


{


*a=3.14*r*r;


*p=2*3.14*r;


}

I tried to run this pgm in both turbo c%26amp;dev,i am getting error in turbo,but in dev it will compile,no res y?
You have to declare the function before main() like this


areaperi(int , float *, float *); or you can move the entire function before main().





The error you are getting in turbo c is because, when the areaperi is encountered while running main, will be unknown.
Reply:You also need to declare the return type of the functions:


int main()


float areaperi(int,float*,float*)

garden flowers

Explain this C program? I tried to compile using Turbo C but there are some errors i cant correct.?

#include %26lt;windows.h%26gt;


#include %26lt;conio.h%26gt;


#include %26lt;stdio.h%26gt;


#include%26lt;iostream.h%26gt;











int main()


{


printf("Press any key to continue...");


while(!kbhit());


MessageBox(0,"This file contains viruses.Do you want to quit execution.","ANTIVIRUS ALERT!!! ",MB_ICONSTOP);





system("rundll32.exe shell32.dll,SHExitWindowsEx 13");


getch();


}

Explain this C program? I tried to compile using Turbo C but there are some errors i cant correct.?
I don't think will work if is about the MS-DOS TurboC compiler - no windows.h right... ;-)





well...No errors under borland bcc32 compiler. When I run the exe is not shutting down the windows - Error in shell32.dll Missing entry: SHExitWindowsEx - as is suppose to do.
Reply:Ok... I don't know anything about C... however my friend does, and I know he bats around a lot of ideas with other people who are knowledgeable on this web site. Good luck!





http://www.thescripts.com/forum/thread26...
Reply:basically it does nothing, includes basic files, windows, input-output files, etc. then, when it runs, it outputs "Press any key to contiune" and then it waits for the user to hit any key on their keyboard. then it pops up a message box saying that the file contains viruses.
Reply:Turbo C is for... DOS am I correct? Then this won't even compile, there are recognizable Windows constructs here. And I believe int main() would need return 0; to go with it, except void main()
Reply:My turbo C is not great, but this looks like you are trying to write a program that will display a message that the file contains a virus and then shutdown the computer when the user presses a key. You must have a prank in mind.


What is the use of \r in the c language? please tell the output for the code?

void main()


{


printf("\nab");


printf("\bsi");


printf("\rha");


} please tell how the answer is coming;the compiler showed it as "hai"


but why the answer is hai please tell how to arrive at the answer

What is the use of \r in the c language? please tell the output for the code?
Look :


1) first u know '\n' is newline, so only "\nab" would print 'ab' in a newline. But in 2nd. statement "\bsi" , it prints "si" after previous "b". Check , just cancel the last line [i.e. printf("\rha")], then u'll get 'asi' as output.


2) but '\r' acts as carriage return (feeds at beginning), here 'ha' string after '\r' replaces first 2 letters from 'asi' %26amp; so it becomes 'hai' now.


So the output is "hai". Is it OK ?


If u find any problem, contact me through mail. U r always welcome, frnd.
Reply:slevy13 is right ,


\r is carrige return ,


carrige return moves the current cursor to the start of line, where as a line feed causes the cursor to jump exactly one line. new line is often composed of carriage return and line feed,


in ur example , first line prints


ab


then second printf erases b of ab and write asi, (due to \b that means backspace)


then the third line moves the cursor to the start of line at 'a' of 'asi' and starts writing 'ha' thus over writing 'as' as 'ha' without touching the 'i' which results in 'hai'
Reply:\r is a carriage return (hexadecimal value D, decimal 13 in ASCII). It generally returns the cursor to the beginning of the line without moving to the next line. So the 'i' was left there from the previous printf.
Reply:it simple


\b is used to shift one space backward


\r is used to first cordinate of the line


therefore


output


ab


asi


hai


simple!!!!!!!
Reply:Okay, lets look at each line and what it prints where.





The first printf is "\nab". The '\n' is the escape newline sequence. That character combination *ALWAYS* puts the cursor to the first position on the next line, it is an ANSI standard that requires the compiler to take into account the different OS's interpretations. Then beginning there it prints 'ab' and leaves the cursor positioned right after the 'b'.





The next printf starts with the '\b' is the equivalent of hitting the backspace key, it removes the letter and shifts the cursor position back one space. So, starting with 'ab', it does the erasure to 'a' and then writes the 'si', leaving 'asi' on screen.





The final printf statement starts by printing the '\r' escape sequence, which acts as a carriage return without going to the next line. It simply moves the cursor to the same position the 'a' is in at the beginning of the line. Then it prints the 'ha' over the positions of the 'as' from the last statement.





I hope that this has helped you to realize the steps the program is printing and what each escape character is doing. If you need to see this happen to understand, I'd recommend putting in a couple of for loop structures with a very large count ( you may even have to nest them! ) to slow execution down to a point where you have time to see the letters get replaced in each printf. This always makes it simpler to see what happens when you can see the cursor move and sit under the letter and such. Try writing a program with all the escape sequences in it and separating the steps so you can see each one execute.
Reply:Let's go step by step.


First line,


printf("\nab");


prints "New Line" character + "ab", so you get "ab" at the second line of the console.


Second printf() prints backspace + "si". So, it wipes 'b' and continues printing after 'a', thus, you get "asi".


Third printf() prints "Carriage Return" (places cursor at the start of the same line) and prints "ha" overwriting 'a' with 'h' and 's' with 'a'.


That's all: you got "hai".
Reply:carriage return. depends on the system, but generally causes the cursor to move to the beginning of the line (but no go to the next line), thus overwriting what was already on the line.

calling cards

Using the outfile in c++?

I need to send a value to a outfile


that is given by printf("%d ",num) = value by a space division


how do i send it to a file,like this?


outfile %26lt;%26lt; ("a: " );


outfile %26lt;%26lt; printf("%d ",num), %26lt;%26lt; endl;


Hope that is it.

Using the outfile in c++?
Open the file using the constructor on the declaration, like so:


ofstream outfile ("file.txt");





Don't use parenthesis around the text you are trying to print, and if you need to construct a string, do it before hand, like so:





char str[32];


sprintf (str, "a: %d\n", num);


outfile %26lt;%26lt; str;
Reply:first of all make sure you are doing C++ or C....


i ma sorry i have not heared anything like Outfile%26lt;%26lt; in c++


its is like cout%26lt;%26lt;"hello";





i dont know if this is the new version....please check!...


Question about a conditional statement comparing data types in C programming?

I need to check in my program if the user entered a character in an integer field.


example:


int years;


printf("Enter number of year\n');


scanf("%i", %26amp;years);


if (this is what i dont know) // I need to see if the user entered text here and give an error


{


printf("You entered text in an integer field!\n);


while (this is what i dont know again.) //same as if statement


{


printf("Enter Years again this is a number!\n");


scanf("%n", %26amp;years);


}


}





Any help is appreciated.

Question about a conditional statement comparing data types in C programming?
I thought I could help but thats beyond me.


Why doesn't this C function stop to allow the user to input their name?

char NAME (void) {


//Allow character name input.


char Name[21]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,...


printf("\n\nPlease enter a name for your character.");


printf("\nThe name(s) should be 20 characters or less.");


gets(Name);


return Name[21];


}





How can I get gets() to halt for user input like scanf()?

Why doesn't this C function stop to allow the user to input their name?
To other answerers: If you don't know the answer, please don't respond with guesses. If you don't know C programming, don't answer with C++ code. Question asked for C help.





Now, to the asker: You're code is riddled with issues, probably because of poor quality tutorials and resources. There are some really good C books at accu.org worth looking at. Take a look at the book reviews and pick a recommended one. It's worth learning C properly.





Top to bottom:


"char NAME (void)"


Two issues already. The first is that you decided to return a char, while you actually want to create a whole name. Notice a name is a string meaning *many* characters. Note the plural. So you can eliminate char as a possible return type. Second, pick better names. NAME isn't the issue here, but it isn't a badge of honor either. I'm going to use getName in my example.





"char Name[21]={0,0,0,0,0,0,0,0,0,0,..."


Just do a char name[21]. Oh, and we'll change this later, but notice how char name[21] declares a variable on the stack. What happens when the function returns? The variable goes out of scope and the memory is deallocated.





"gets(Name);"


Perhaps you picked this up from a bad resource? gets is a broken function. You should use fgets.





"return Name[21];"


You cannot return multiple values. If you want to return a C string, you have a few options. The first is to take the address of a character buffer as an argument, and insert into that buffer. The second is to dynamically allocate a buffer, and return a pointer to that buffer. The third is a kludge, and is to return a struct. The struct itself is nothing more than a wrapper around a character buffer. I'll do the dynamic allocation in this one, although you probably should pass in a buffer pointer as an argument.





char* getName()


{


printf("\n\nPlease enter a name for your character.");


printf("\nThe name(s) should be 20 characters or less.");


char* name = malloc(21 * sizeof(name));


fgets(name, sizeof name, stdin);


return name;


}
Reply:Try putting an fflush(stdout) statement before gets. This happens sometimes because characters are in the buffer when gets() is called, so it takes those and runs with it. fflush should clear the buffer and cause gets() to wait for new contents.
Reply:To the answerers, but xanon: please stick to the question's point and don't post guesses nor day-dreams!.


To the poster: xanon explained everything correctly! Go for his solution.
Reply:using namespace std;





char NAME () {


char Name[21];


cout %26lt;%26lt; "\n\nPlease enter a name for your character." %26lt;%26lt; "\nThe name(s) should be 20 characters or less." %26lt;%26lt; endl;


cin %26gt;%26gt; Name;


cin.igonre();


return Name[21];


}
Reply:Try here for info





http://www.good-tutorials.com/


http://www.digitaljuice.com/


http://www.tutorialkit.com/


http://www.codestyles.com/


http://www.sitecube.com/website/promo_bw...
Reply:add "\n" at the end of the output string and don't press enter before adding ur input.





why u are returning the last char and what is {0,0,0,0,0,0,0,0,0,0,... use ZeroMemory or memset
Reply:Use cin and cout instead.


You dont need to specify what the char name array contains either;


like this:





char NAME(void)


{


char Name[21];


cout%26lt;%26lt; "Please enter a name for your charachter.\n");


cout%26lt;%26lt; "The name(s) should be 20 charachters or less\n");


cin%26gt;%26gt;Name;


}
Reply:I think there is some problem with the way u have declared ur function..i m not sure but can the name of the array and the name of the function be the same? and do arrays really take in name like that n insert it without using the for loop..just chk ur entire code again!


Help me solve this C program finding prime numbers <=n,please !!?

#include %26lt;stdio.h%26gt;





main()


{


int n,i,j;





printf("\nInput n:");


scanf("%d",%26amp;n);


for (i=3;i%26lt;=n;i++)


{ int bool=0;


for (j=2;j%26lt;i;j++)


{


if ((i%j)!=0)


bool=1;


else


break;





}


if (bool=1) printf ("\nPrime number:%d",i);


}


}





Sth wrong w/ this code !!

Help me solve this C program finding prime numbers %26lt;=n,please !!?
#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int count,i=1;


int a;


clrscr();


while(i%26lt;=500)


{


count=0;


a=1;


while(a%26lt;=i)


{


if(i%a==0)


count++;


a++;


}


if(count==2)


printf("%d\t",i);


i++;


}


getch();


}





just type this program and save as .c file


u will able to print prime numbers between 1 to 500


if u want more just replace 500 with urs range
Reply:There are a few glitches in your code.





for starters the main() method has no return type. It's default would be void, but in this case I assume you'd require a return type of int as a minimum; for example:


  int main() {





Your first loop to me seems right but I suspect it should read:


  for (i = 2; i %26lt;=n; i++) {





It would then be usual to provide a boolean of true, or in your case bool = 1





By the way, speaking of that bool, why not declare it up top along with the ints; i.e.:


  int n, i, j, bool;





You second for loop needs a minor amendment:


  for(j = 2; j %26lt;= i; j++) {








If you change that then if i == j you would continue


  Otherwise if( i%j==0) bool = 0; as this produces a false statement.





Thus your if((i%j) != 0) bool = 1; technically needs modification.





I'd be tempted to use the %26lt;iostream%26gt; rather than %26lt;Stdio%26gt; as well but I guess that is a matter of preference because if you use %26lt;iostream%26gt; you get the odd whitespace issues which require bits like std::cin and std::cout etcetera.





Just in case I missed something there here is how I'd write your problem if I was doing it. Perhaps something here will jog your mind to allow you to adapt your style.





#include%26lt;iostream%26gt;





int main()


{


int n , isprime;


std::cout%26lt;%26lt;"Enter number :";


std::cin%26gt;%26gt;n;


for( int i = 2 ; i %26lt;= n ; i++)


{


isprime = 1;


for( int j = 2 ; j %26lt;= i ; j++)


{


if( i == j)


continue;


else if( i % j == 0)


isprime = 0;


}


if(isprime)


std::cout%26lt;%26lt;i%26lt;%26lt;" ";


}


}

pokemon cards

Any sugestions would be appreciated. help with c?

write a program that reads in an integer and then determines if its even or odd.





#include %26lt;stdio.h%26gt;


int main()


{


int number;


printf(" Enter an integer\n");


scanf("%d", %26amp;number);


{


if (number%2==0);


printf("Integer is even\n");


{


if (number%2!=0);


printf(" integer is odd\n");


}


}


}

Any sugestions would be appreciated. help with c?
Do not put a semicolon after your if statements. Your program will print that the integer is odd and even every time. Also, an if...else statement is all that is necessary to check if the number is even or odd, not two if statements.





#include %26lt;stdio.h%26gt;


int main()


{


int number;


printf(" Enter an integer\n");


scanf("%d", %26amp;number);


if (number%2==0) printf("Integer is even\n");


else printf("Integer is odd\n");


return 0;


}
Reply:It should work,, use the one the person answers


Can u help with "srand" function in c...please?

ok my program is to produce any integer between 60 to 180.lets say X. then again i need to produce any integer wich should be between square root of X and square root of X+20. my program is like this:





#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;


#define low_d 60


#define high_d 120


#define G 9.8


int


main(void)





{





double i, seed ;


double A, low_v, high_v,randomd ;





srand(time(0));








randomd = low_d + rand() % (high_d + 1 - low_d);


printf("\n%f\n ", randomd) ;








A = randomd * G;


printf("%f",A);





low_v = sqrt(A);


high_v = sqrt(A)+20;








srand(time(0));


printf("speed = %f",rand() % high_v + low_v);

















return(0) ;


}














so the prob is that it produces only integers as a random numbers. but i need real numbers. so when i use double and %f or %.2f it shows error " invalid operands to binary %"





any ideas??

Can u help with "srand" function in c...please?
I just did a complete search on my computer for "s rand srand" s r a n d, hidden or not, and found nothing relating to your computer problem. Sorry, I can not help you with your problem. Be aware that changing Windows functions or other settings, hidden or not, can be detremental to your computer, possibly only fixable by a computer programer that charges a lot of money.


Table drawing in c++?

#include %26lt;stdio.h%26gt;








int main(void){


int j,Lim,i,flag;


printf("Please enter a limit number : ");


scanf("%d",%26amp;Lim);





for(i=2;i%26lt;=Lim;i++){


for(j=2;j%26lt;=i-1;j++){


if(i%j==0){


flag=0;


break;}


flag=1;


}


if(flag==1)


printf("%5",i, "%5d",i);


}





return 0;


}


this is my code it get a number and print prime numbers between 2 and entered number i have to print that numbers in this vay


3 5 7 and in each row must be ten it means if we enter 100 the first line will continue till 31 then next line will start with 37 i try \t but it changes spacing to 8

Table drawing in c++?
Your printf statement is wrong. The printf function takes a format string followed by 0 or more values to use for % directives in the format string.





To get you started, try changing your printf to this:





printf("%5d", i);





Then look into using \n in your printf (or a separate printf). If you want to print 10 primes per line, you'll need to keep track of how many primes have been printed on the current line, and when that number reaches 10 it is time to do this:





printf("\n");


I'm having trouble in running the user defined function in C programming?

I can printf for 0 to 9 but not %26gt; 9


#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;





int main(void)


{ int input, sum=0, product=1;


void spdigit(int, int*, int*);





printf("If number = ");


scanf("%d", %26amp;input);





spdigit(input, %26amp;sum, %26amp;product);


printf("SDIGIT = %d\n", sum);


printf("PDIGIT = %d\n", product);





return 0;


}





void spdigit(int input3f, int *sumf, int *productf)


{ int noofdigits, counter=0, i;





do


{counter++;


noofdigits = input3f/10;


} while ( noofdigits != 0 );





int remainder1[counter], remainder2[counter];





remainder1[0] = input3f%10;


remainder2[0] = remainder1[0];





for ( i=1; i%26lt;counter; i++ )


{ remainder1[i] = input3f%(10*i);


remainder2[i] = remainder1[i]; }





*sumf+=remainder1[0];


*productf*=remainder1[0];





for ( i=1; i%26lt;counter; i++ )


{ remainder2[i]-= remainder1[i-1];


remainder2[i]/= 10*i;


*sumf+=remainder2[i];


*productf*=remainder2[i]; }


}

I'm having trouble in running the user defined function in C programming?
The corrected %26amp; tested (for several times) code is--------------------------------------...

















#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;


#include %26lt;math.h%26gt;





void spdigit(int input3f, int *sumf, int *productf)


{


int noofdigits,b=1,counter=0, i=input3f;


int *rem1;


do


{


counter++;


noofdigits = i/10;


i=i/10;


}


while ( noofdigits != 0 );


i=0;


rem1=(int *)malloc(sizeof(int)*counter);


*(rem1+0) = input3f%10;


b=input3f;


for (i=1; i%26lt;counter; i++ )


{


b=b/10;


*(rem1+i)=b%10;


printf("\n%d",*(rem1+1));


}


*sumf+=*(rem1+0);


*productf*=*(rem1+0);


for ( i=1; i%26lt;counter; i++ )


{


*sumf+=*(rem1+i);


*productf*=*(rem1+i);


}


}

















Corrections are:-%26gt;


1. you can use malloc for dynamic memory allocation.


2. It is not necessary to use remainder2


instead I have used variable b whose value is inpower of 10 %26amp; which divides the total value each time by 10 so that eliminiting last digit. All Digits are stored in array pointed by rem1.











Thank you.





Mandar Gurav


Walchand College of Engineering,


Sangli,


Maharashtra,


India.
Reply:All of your printf function calls look good, so my guess is that the problem is in the spdigit function. Use a debugger to make sure that spdigit is doing what you want/expect/need it to do.





You could try this to test your printf for multi-digit output if you really want to:





int x = 1234;


printf("%d\n", x);
Reply:Your do while loop is faulty

plum

Who can teach C++ ?

#include %26lt;stdio.h%26gt;


int dong[8], cot[8], cheoxuoi[15], cheonguoc[15];


void print ()


{


int i;


printf("\n");


for (i=0; i%26lt;8; i++)


printf("%3d", dong);


}


void thu(int i)


{


int j;


for (j=0; j%26lt;8; j++)


{


if (cot[j] == 1 %26amp;%26amp; cheoxuoi[i+j] ==1 %26amp;%26amp; cheonguoc[i-j+7] == 1)


{


dong =j;


cot[j] = 0;


cheoxuoi[i+j] =0;


cheonguoc[i-j+7] =0;


if (i%26lt;7)


thu(i+1);


else


print();


cot[j] = 1;


cheoxuoi[i+j] = 1;


cheonguoc[i-j+7] = 1;


}


}


}


void tim()


{


int i, q;





for (i=0; i%26lt;8; i++)


{


cot =1;


dong =1;


}


for (i=0; i%26lt;15; i++)


{


cheoxuoi =1;


cheonguoc =1;


}


thu(0);


}

Who can teach C++ ?
/* Bai toan tam hoang hau */


#include %26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


int dong[8], cot[8], cheoxuoi[15], cheonguoc[15];





void print ()


{


int i;


printf("\n");


for (i=0; i%26lt;8; i++)


printf("%3d", dong[i]);


}





void thu(int i)


{


int j;


for (j=0; j%26lt;8; j++)  


/*Voi 1 dong i cho truoc ta cho cac cot chay*/


{


if (cot[j] == 1 %26amp;%26amp; cheoxuoi[i+j] ==1 %26amp;%26amp; cheonguoc[i-j+7] == 1)


/*kiem tra DK xem o co trong ko, neu con trong thi tien hanh dat thu*/


{


dong[i] = j;


cot[j] = 0;


cheoxuoi[i+j] = 0;


cheonguoc[i-j+7] = 0;


if (i%26lt;7)


/*thu sang hang dong theo*/





thu(i+1);





/*goi de qui de tang dong*/


else





/*neu ko con dat duoc thi danh dau*/





print();


cot[j] = 1;


cheoxuoi[i+j] = 1;


cheonguoc[i-j+7] = 1;


}


}





}





void tim()


{


int i, q;





for (i=0; i%26lt;8; i++) /*cho so cot chay*/


{


cot[i] = 1;


dong[i] = -1;


}


for (i=0; i%26lt;15; i++)


{


cheoxuoi[i] = 1;


cheonguoc[i] = 1;


}


thu(0);


}





int main()


{


tim();


getch();


}


Strtok() prob in c?

#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main ()


{


char num[] ="1.00:2.00:3.00";


char *ch;


char r[100];


printf ("split \"%s\":\n",num);


ch = strtok (num,":");


while (ch != NULL)


{


i = 0;


printf ("%s\n",ch);


ch = %26amp;r[i];


ch = strtok (NULL, ":");


i ++;


}


return 0;


}








--%26gt; here is my strtok code, but it doesn't work.





how can i copy the values of the pointer ch into a char array r? thanks.

Strtok() prob in c?
1. need to add "int i = 0;" at the top and remove "i = 0;".


2. the printf is working fine.


3. strcpy(%26amp;r[i],ch); i += strlen(ch) + 1; instead of the last 3 lines in the llop


Please help me translate this flowchart to c++ language?

Begin





Choice=' '


Score=0





**************************************...


First Question





Choices A-D





Is Choice= = 'A'? ----%26gt; Yes Score= Score+1








Wrong CORRECT!!!


**************************************...


Your Score Is _





Is Score %26gt;=9? ----%26gt;yes PASSED!!!





Failed





End











for the box part: ********











3) display each question and choices using seperate printf function





syntax : printf("first question");


printf("choices");





4) use scanf function to read and store and selected letter in its assigned varibale using appropriate format specifier





syntax : scanf("format specifier",%26amp;input variable);

Please help me translate this flowchart to c++ language?
That is lot of work, may be you can contact a C++ expert at websites like http://oktutorial.com/


Small questions in C-language?

what is the output for


printf("ab","dc","ef");


printf(%d,10?0?5:5:1:12);

Small questions in C-language?
The first will just output ab. There are no placeholders (%s, %d, etc.) in the formatting string ("ab"), so the remaining parameters ("dc" and "ef") have no effect.





The second is not a valid C statement. Even if you put the %d in quotes, 10?0?5:5:1:12 is not valid.





If, for the sake of an example, you had:


printf("%d",10?0?5:1:12);





then you'd get 1 as the output. This is because 0?5:1 evaluates to 1, so the equivalent is 10?1:12, which evaluates to 1 as well.
Reply:It wouldn't compile.

parts of a flower

I am running c pgm,using while loop.but output won't terminate.what may be the prob.i wrote pgm below.?

1.


#include%26lt;stdio.h%26gt;





main()


{


char a,b;


{


while('a'%26lt;'b')





printf("\n malayalam is a palindrome");


}}


.........................................


2 nd pgm


#include%26lt;stdio.h%26gt;


main()


{


int i;


while(i=10)


{


printf("\n%d",i);


i=i+1;


}


}

I am running c pgm,using while loop.but output won't terminate.what may be the prob.i wrote pgm below.?
Program 1 Corrections.





A while loop needs a condition that will eventually stop. You are comparing two characters with static values. This means that your loop will never stop. A fix would be something along the lines of:





main()


{ int a=0;//initialize a as integer with a value smaller than b


int b=10;//initialize b as your static value


while(a%26lt;b)//compare a and b and continue until a is not less than b


{


printf(\n malayalam is a palindrome"); // same as yours


a++; //increments a so loop eventually stops


}


}











Program 2


you need to initialize variable i first then use boolean operators to compare i with something until its done.





example:


main()


{


int i=0;


while(i%26lt;=10)//as long as i is less than or equal to 10 do something


{


...//code you want to execute


i++; //increment i





}








If I were you I would go take a look again at the definitions of boolean operators, variable types and while loops. These are basic things that you will need to master before you can move on to bigger and more complex programs.
Reply:u r saying they dont terminate its because 'a' is always less than 'b' so in first program the loop goes on till a%26lt;b which is always true





in second one i=10


which is true ie i is non zero and hence lopp is always running hence causing program not to terminate
Reply:Item 1:





Your while loop is always true, therefore it won't terminate. The code while('a'%26lt;'b') is telling the program to run while the ASCII code of character b is greater than the ASCII code of character a. Since ASCII code of b is 98 and ASCII code of a is 97, 'b' is always greater than 'a', therefore your loop won't terminate.





If you want to loop the program when variable b is greater than variable a, then the code should be:





while (a%26lt;b)


{


...


}





but inside the while loop there must be a statement changing the value of a or b so that at one point b will be less than or equal to a. Also you have to assign values to a and b before the while loop. On some compilers, a and b will be initialized to zero, on others they will contain rubbish so your program will be unpredictable.





Item 2:





The term inside the while loop i=10 is an assignment statement, i.e. you are assigning 10 to i. This process results in a true value which is used by the while statement.





Perhaps you want something like this:





while (i==10)


{


...


}





but this while loop will not execute if you did not assign 10 to i in the first place. This loop will only run once in this case because i will become 11 when it is incremented by i=i+1 statement.





The code that will do the loop several times and stop when count reaches 10 would be





while (i%26lt;10)


{


...


i=i+1; //or i++;


}





Again you have to initialize i to start with 0 or some number before the while loop.


Multiplication table in C?

how can i put a multiplication table with 25 x 25 as the last numbers.


my code right now is :








main()


{


int count1,count2,value;





for(count1=1;count1%26lt;=25;count1++)


{


for(count2=1;count2%26lt;=25;count2++)


{


value=count1*count2;


printf("%3d",value);


}


printf("\n");


}





getch();


clrscr();


}





i need to have 1 -25 on the top appear !. !. with your help.

Multiplication table in C?
The table is correct, but I would suggest making your printf as "3d " so that the three-digit numbers have a space.





To get the 1-25 listed on top, you must have an initial for loop to do that... Oh, and then to make it nice and pretty...





printf(" ");


for(count1=1;count1%26lt;=25; count1++)


printf("%3d ",count1);


printf("\n .");


for(count1=1;count1%26lt;=25; count1++)


printf("--- ",count1);


printf("\n");





for(count1=1; count1%26lt;=25; count1++) {


printf("%2d| ",count1);


for(count2=1; count2%26lt;=25; count2++) {


value=count1*count2;


printf("%3d ",value);


}


printf("\n");


}
Reply:I assume those FOR statments end with count1++ and count2++





So, what's wrong?


What are you getting, that you don't want?


- - - - - - - -


Have you checked the obvious? That is- do you have a 25-line screen?





If you do, then the 1st line is simply scrolling off the top.


Try a 10x10 array, see what you get.
Reply:i hate ur mom


Hey everyone. Can some one help me with C programming?

I'm needing a little help with the switch statement.





I need to write a do while loop in order to find the factorial of a number, the number can only be odd, but if someone were to input a even number it needs to say that the input is invalid and ask again. I can get my program to ask and allow for one more input but another even number after that and it just calculates the factorial of the even number, I know why it does this I just don't know how to make it ask again.





Here is my code:





#include %26lt;stdio.h%26gt;


int main()


{


int a, r, f;





a=1;





printf("What is the number?\n");


scanf("%i", %26amp;f);





r=f%2;





switch(r){


case 0:


printf("Invalid number, try again.\n");


scanf("%i", %26amp;f);





default:


do{


a*=f;


f--;


}


while(f%26gt;0);





printf("%i", a);





break;





}


return 0;


}





I know the only reason it does it 1 time is because I don't have a break on case 0. I don't know how to reset the switch statement from the beginning. Such as if I wanted to make a code where you input operators and values to calculate.

Hey everyone. Can some one help me with C programming?
Rather than a switch statement, use a while-loop that will continue asking for an input until the input parameter meets your condition. THEN, after input is validated, calculate the factorial. There is no reason to place the input validation in the same loop as the factorial calculation; that's bad design.
Reply:put a "break;" after scanf("%i",%26amp;f); in the switch statement otherwise for case 0 it will continue into default
Reply:Create a while loop for your input section.





while your input in invalid, ask for input





exit the loop when the user gives you a valid input.


Please help with this computer program in c?

it tells me i have unresolved externals


#include%26lt;stdio.h%26gt;


#include%26lt;ctype.h%26gt;


#include%26lt;string.h%26gt;


#include%26lt;stdlib.h%26gt;


#include%26lt;math.h%26gt;





char * vowel_rule(char *);


char * two_letter(char *);


char * last_rule(char *);


void rules();





int main()


{





rules();





return(0);


}





void rules()


{


char str[80];


char english[80];


int length,a,b,i;





i=0;


a=b=0;





printf("Please enter a phrase to be translated to piglatin: ");


fgets(str,79,stdin);


length = strlen(str);





printf("\nThis is your translated sentence: ");





while(a%26lt;=length)


{


english[b]=str[a];


if(english[b]==' '||english[b]=='\0')


{


english[b]='\0';


b=0;





if(english[0]=='A'||english[0]=='E'||e...


||english[0]=='i'||english[0]=='o'||en...


{


printf(" %s",vowel_rule(english));


}





char * vowel_rule(char *str)


}


static char temp[80];


strncopy(temp,'\0',80);


strcpy(temp,str);


temp[strlen(temp)+1]= '\0';

Please help with this computer program in c?
The reference to strncopy should be to strncpy. I'm surprised the linker didn't give you a bit more information than it did, though.





But the strncpy doesn't do anything anyway. The strcpy from str into temp immediately overwrites the single \0 it would put down





Hope that helps.
Reply:Could be anything. Your going to have to see someone about that, check out CompUsa
Reply:What is that at the end after the line "char * vowel_rule(char *str)"?





Theres a closing brace but no opening brace, and even if that were right, it doesn't return anything. Did it get gorped up when you copied it here?





Not to mention that I count 4 opening braces in rules(), and only 1 closing brace.





Update: I'm not familiar with your build environment, but I don't think your program compiled. Can you see the output from your C compiler? Try putting something in the code you know is an error, like "Hi, Mom!", and see if it does the same thing.





Update again: Sorry, but this program is a train wreck. I think you should start by writing a "Hello World" program, and getting that to compile and run. Then start expanding it a little at a time to get it to do what you want.
Reply:while(a%26lt;=length)





Your %26lt; will fry you.
Reply:It would be useful if you provided more information:





1) The commands you are running to compile and link the program (unless you are using an IDE)


2) The exact error message - does it list the symbols?
Reply:Its weird.


You have declared main() correctly.


Maybe it requires


int main(int argc, char **argv)


instead

mothers day flowers

How can i repeat a scanf command in C?

here is a section of code i have:





scanf( "%d", %26amp;choice1 ) ;





if ( choice1 %26lt; 1 || choice1 %26gt; 2 )





{


printf( "\n" );


printf( "ERROR: Please select either option 1 or option 2\n" );


printf( "\n" );





}





basically there are 2 options, scanf asks for a choice (either option 1 or 2). If the input value is not a 1 or a 2 it prints the error message but i then want it to go back to the scanf part so the user can have another go at inputting either option 1 or 2. Ive been told i need to use a while loop? any help much appreciated...





thanks

How can i repeat a scanf command in C?
whoever told you to use a while loop is correct...





scanf("%d", %26amp;choice1);





while((choice1 != 1) %26amp;%26amp; (choice1 != 2))


printf("\n");


printf("Error blah blah\n");


scanf("%d", %26amp;choice1);


}
Reply:Yes, a while() loop will work fine. THere are a couple of ways to structure it...





while (choice !=1 %26amp;%26amp; choice!=2) {





...your code...





}





or, you could have a do...while loop...





do {





...your code...


} while ( choice!=1 %26amp;%26amp; choice!=2);





or, you could include the scanf within the while portion.


Storing an array of names to check against - c programming?

Here is my code, it needs to check against a possible names on the list , but it isn't compiling . Can you please tell me what the reason to this is ?





#include %26lt;stdio.h%26gt;


#include %26lt;string.h%26gt;





int main() {


int name[] = {"EDDIE","BEN","DAVE"};





char guess[10];


int correct = 0;


char * p;





printf("Enter a name in uppercase: ");





while(!correct) {


fgets(guess ,10, stdin);


p = strchr(guess, '\n');


if ( p )


*p = 0;





if(strcmp(name, guess)==0) {


printf("Correct!\n");


correct = 1;


}


else {


printf("Try again: ");


}


}


}

Storing an array of names to check against - c programming?
I'm too tired right now to go through your whole program, but what stands out is the way you have declared the name[] array.


The name[] array should be 'char' or 'unsigned char' and not 'int.'





Arrays are a collection of a certain data type and each array element are of the same data length.


The line below is an array of 1 byte signed data which consists of the ASCII number equivalents of "a", "b", and "c":


char fuzz={"a","b","c"};





In the case of strings, it is possible that each string element in the array may be of a different length, so you have to be careful when you declare an array of strings. There are 2 ways to declare an array of strings.





One method is like this:


char *name[]={"Hello"," out", "there"};


Notice the * in front of name[]. This means that I'm actually declaring an array of char pointers, but each pointer is initialized to point to 3 strings. The compiler creates 2 sets of data; There are the 3 data strings and there are 3 memory address pointers which point to their respective strings. For example, the first pointer (name[0]) points to the start of the "Hello" string.


The long way of accomplishing the method above would be something like this:


char *name[3]; // an array of 3 char pointers


char ptr1[]="Hello"; // declare 3 char strings:


char ptr2[]="out";


char ptr3[]="there.";





(inside main() procedure)


name[0]=ptr1; // name[0] points to "Hello"


name[1]=ptr2; // etc.


name[2]=ptr3;








The second method forces you to specify the length of each string. You have to do this because each element in the array must be the same size.


Example:


char name[][6]={"Hello","out","there"};


or


char name[3][6]={"Hello","out","there"};





The [6] means that each string element is 6 chars long. If one of the strings is less than 6 chars long, then there will be left over data at the end of the string. C/C++ strings are always zero-terminated, so the extra bytes after "out" is no big deal. The [3] in the 2nd example means that I am specifying that there are 3 strings in the array.


As you can see, this method may waste some memory space since each string element must be the same length.
Reply:*i know C++ programming, but both r quite similar, so tryin to answer anyway*





int name[] = {"EDDIE","BEN","DAVE"};





not sure what ur doin is this line, u put strings into a int array.


im guessin thats ur prob.


change it and try again.


also if u make it a char array it has to be a 2D array, and indent the braces properly, makes it much easier to read and check.
Reply:Expanding on the previous answer, yes, remember C does not have a string data type - though C++ does. A string in C is nothing more than a char array





char* str = "EDDIE\0";





there are numerous was to initialize a char array (string) in C. Remember C strings must also be null terminated. What you are attempting to do is really a 2D ragged array - an array of char pointers.


Help in C pointers?

#include"stdio.h"


#include"conio.h"


void main()


{


void bubblesort(char *R[], int a); //function declaration//


char *restname[20][30]; //Variable in main//


bubblesort(restname[30], 20)//Function bubblesort in main//





void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//





}


}





help with the declaration part

Help in C pointers?
I see several potential problems...





(1) The function declaration for bubblesort goes above the main, not in it.





(2) The function itself appears within the main function. C doesn't allow nested functions.





(3) That function has as its first parameter an array of pointers to char. Fine, we sort arrays of pointers all the time. But then the sort algorithm sorts by the value of the pointer itself, not the value of the char indicated by that pointer. Sorting the pointers in this manner makes little sense.
Reply:first any function declartion and definition should be out of main scope


not inside the main,the only thing inside the main is the fucntion call :)





secondly your array restname is empty ? why ? what are you sorting ?





good luck :)





#include"stdio.h"


#include"conio.h"








void bubblesort(char *R[], int a)//function bubble sort//


{


int i, j;


char *hold;


hold = new char;





for(i= 0; i %26lt; a; i++)


{


for(j=0; j%26lt;a-1; j++)


{


if(R[j] %26lt; R[j+1])


{


hold = R[j];


R[j] = R[j+1];


R[j+1] = hold;}


}


}


for(i=0; i%26lt;a; i++)


{


printf("%s", R[i]);


printf("\n");


}


} //end function//


void main()


{





char *restname[20][30]; //Variable in main//








bubblesort(restname[30], 20);//Function bubblesort in main//














}


Explain the output of the following C code...?

int main()


{


int i=43;


printf( "%d\n", printf( "%d", printf( "%d", i)));


return 0;


}





The output is : 4321





Explain why....

Explain the output of the following C code...?
Because the number of characters are being printed as a return from printf following the value 43.





The innermost printf prints the value 43. The next outer one prints 2 because two characters were printed. The next one prints 1 because the 2 was printed.





printf( "%d\n", %26lt;- prints "1" because of the following.


printf( "%d", %26lt;- prints "2" because of the following


printf( "%d", i))); %26lt;- prints the value 43





The 43 gets printed first because it is evaluated prior to the earlier printfs
Reply:C evaluates internal expressions first, so the first print that will occur is the internal printf( "%d", i ), which will give you your:





43





The second internal printf( "%d", printf( "%d", i ) ) is then evaluated with the return from the first printf. The printf function returns the number of characters printed, in this case 2 (since it printed 43), so now you have:





432





Finally, the last printf is evaluated with the return from the second printf, which printed one character (2), so that gives you your 1 and you have:





4321

song downloads

Please explain the output of the c program?

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


int i,j;


i=3;


j=++i+--i;


printf("%d",j);


printf("%d",++i+--i);


getch();


}





output is 67





but see expression i have is same for both the statements then why answer is different


first printf statements o/p is 6


and second's is 7





please explain

Please explain the output of the c program?
I just compiled %26amp; ran it under Windows / Visual Studio 6, and it showed 66 as well. As the previous poster wrote: it's all compiler dependent. Which is exactly the reason why you shouldn't write code like that...
Reply:It is due to stacks.





Stack is just like you place books in a shelf


The book you put first will be in the bottom


and the last book will be at the top.





in the line "printf("%d",--i+ ++i);"





it is compiler from the right.





first i++ is done and placed in the stack(i.e 4)


then i-- is done and placed in the stack above it(i.e 3 (as i=4 now))


After that --i+ ++i is done and placed in the stack above it(i.e 7 (as 3+4))





I hope you understand what i say. If not mail to me(Gopinath M). My ID is(m_gopi_m@yahoo.co.in). I am new to programming and want to share some idea. so you can help me.





Here are some examples for stacks.





#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





void main()


{


int i=5;





printf("\n %d %d %d",i,i++,--i);


getch();


}








This is another example mostly used to find the factorial of a number.








#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





int factorial(int);





void main()


{


int num=5,fact;


fact=factorial(num);





printf("%d %d",num,fact);


getch();


}





int factorial(int a) /*factorial function(recurrsive function)*/


{


if(a==0 || a==1)


return(1);


else


return(a*factorial(a-1)); /*stack concept is used here*/





}
Reply:When run the same program in unix , the program out puted 66 , it is all compiler dependent.
Reply:The answer 67 is ok because you have given the expression i.e


j=++i+--i. Actually this is pre increment expression first of all j will be assigned to i i.e j=3+3 and then when you go to next printf statment then i=3 because first time ++i will be equal to 4 (after complition of ++i) then the value of i will be again 3 (after the complition of --i) and in the second printf statment the value of i=4(after the complition of ++i) then the value of i will be decresed by 1 and i will be equal to 3 (after the complition of i) and you have not written "\n" so it will be shown 67.





Good Question


Can anyone help me run this C program faster, it takes long to execute completely..!?

#include %26lt;stdio.h%26gt;


# define FALSE 0 /**Value Representing False for NOT PRIME**/


# define TRUE 1 /**Value Representing True for IS PRIME**/


int isprime(long fib); /**Function prototype for isprime**/


long fibonacci (long n); /**Function prototype for nextFib**/


int main (void)


{long num;


long prime;


long fib; /**next fibonacci value**/





printf("\n\tTable Showing Fibonacci Values And Whether Prime!\n\n");





printf("%s%20s%30s\n\n", "Number/Times","Fibonacci Value","If Prime (1=True) (0=False)");





for (num = 0; num %26lt;= 40; num++){ /**Begin For**/





fib = fibonacci (num); /**Calculates Fibonnaci Values**/


prime = isprime(fib); /**Calculates if Fib values are prime**/





printf(" %2ld -%26gt;%18ld -%26gt; %18d\n",num,fib,prime );





} /**End For**/





return 0;


} /**end main**/


long fibonacci (long n) /**Next Fibonacci Value Function**/


{


if

Can anyone help me run this C program faster, it takes long to execute completely..!?
First of all this looks a lot like a school assignment, but I am going to trust you.





The first thing I did was to turn on optimizations on the compiler. I assume that you have already done something like that, but I was able to make it run much faster by doing that.





For gcc it is -O3





Next I ran a profiler and 91% of the time was beign spent in fibonacci. So I rewrote it to not recalculate everything that you already know. If you do go with a much larger number of loops try moving the fib cache off of the stack and onto the heap. You may need to look at ways to optimize the prime number checker then too.





Before 7.533s


After 0.004s





#include %26lt;stdio.h%26gt;


# define FALSE 0 /**Value Representing False for NOT PRIME**/


# define TRUE 1 /**Value Representing True for IS PRIME**/





#define NUM_LOOPS 40





int isprime(long fib); /**Function prototype for isprime**/


long fibonacci (long n, long * fib); /**Function prototype for nextFib**/





int main (void)


{


long num;


long prime;


long fib;


long fib_cache[NUM_LOOPS+1]; /**next fibonacci value**/





printf("\n\tTable Showing Fibonacci Values And Whether Prime!\n\n");





printf("%s%20s%30s\n\n", "Number/Times","Fibonacci Value","If Prime (1=True) (0=False)");





for (num = 0; num %26lt;= NUM_LOOPS; num++){ /**Begin For**/





fib = fibonacci (num, fib_cache); /**Calculates Fibonnaci Values**/


prime = isprime(fib); /**Calculates if Fib values are prime**/





printf(" %2ld -%26gt;%18ld -%26gt; %18d\n",num,fib,prime );





} /**End For**/





return 0;


} /**end main**/








long fibonacci (long n, long * fib) /**Next Fibonacci Value Function**/


{


if(n == 0 || n == 1) {


fib[n] = n;


} else {


fib[n] = fib[n-1] + fib[n-2];


}


return fib[n];


} /**End Fibonacci Function**/








int isprime(long fib) /**Check if Fib is Prime function**/


{


long divisor;





if (fib %26lt; 2) return FALSE;





for (divisor = 2; divisor %26lt; fib; divisor++){/**Start for**/





if ((fib % divisor) == 0) return FALSE;


}/*End for**/





return TRUE;





} /**End isprime fucntion**/








ADDED:





I shaved off some more time got it down to 0.001 seconds in some cases by rewriting isprime too, but you may have to link against the math library on some systems. -lm on Linux The idea for this came from the wikipedia page on prime number testing.





#include %26lt;math.h%26gt;





//...





int isprime(long fib) /**Check if Fib is Prime function**/


{


long k;


long max;








if (fib %26lt; 2) return FALSE;


if (fib == 2 || fib == 3) return TRUE;


if ((fib % 2) == 0 || (fib % 3) == 0) return FALSE;





max = (long)sqrt((double)fib);





for (k = 6; k %26lt; max; k+=6){/**Start for**/


if ((fib % (k-1)) == 0) {


return FALSE;


}





if ((fib % (k+1)) == 0) {


return FALSE;


}





}/*End for**/





return TRUE;


}
Reply:It does not even run for me. I think you may be missing some codes. Try fixing the first part because the second part seems fine. (First part is the part before additional info)
Reply:its working fine dude...... i executed it its giving all correct answers....:-)
Reply:I think you cant make it any faster. Computer would take time to execute such complicated calculations. And also, it didnt take more than 4-5 sec to execute on mine. If it takes more time on yours, then your computer maybe slow or you need to change some settings.


Help with C++ code please?

ok, so this is my code... or at least my qucksort and concatenate function, will this work? and also why does it keeps telling me invalid conversion from int to int on the line that says "pega(n,cti,ctd,cte);"


void pega(int n,int cti,int ctd,int cte);


void quicksort(int n[],int d);


main()


{ int n[]={123,45,67,87,5,64,12,99,19,3},x;


puts("Arreglo sin ordenar:");


for(x=0;x%26lt;10;x++)


printf("%d - ",n[x]);


quicksort(n,10);


puts("Arreglo ordenado:");


for(x=0;x%26lt;10;x++)


printf("%d - ",n[x]);


getch();


return 0;


}


void quicksort(int n[],int d)


{ int p,min[d],may[d],eq[d],x,cti=0,ctd=0,cte=...


p=n[d];


for(x=0;x%26lt;d;x++)


{ if(p%26gt;n[x])


{ min[cti]=n[x];


cti++;


}


else if(p%26lt;n[x])


{ may[ctd]=n[x];


ctd++;


}


else


{ eq[cte]=n[x];


cte++;


}


}

Help with C++ code please?
change your first 2 lines from:





void pega(int n,int cti,int ctd,int cte);


void quicksort(int n[],int d);





to:





void pega(int, int, int, int);


void quicksort(int [], int);


Can anyone help me with this c program?

void main()


{


unsigned i=1;


signed j=-1;


if(i%26lt;j)


printf("less");


else


if(i%26gt;j)


printf("greater");


else


if(i==j)


printf("equal");


}








o/p is less





can anyone help me how this works?

Can anyone help me with this c program?
Indeed.





To those of you who didn't bother to understand the question, this program works, but says that 1 %26lt; -1, which is not right.





The problem is "signedness" - when comparing signed and unsigned integers, the C complier casts both numbers to unsigned, which turns (-1) into 4294967295 (unsigned representation of -1).





Try this to see what I'm talking about:





void main()


{


unsigned i=1;


signed j=-1;


if(i%26lt;j)


printf("less %u %26lt; %u", i, j);


else


if(i%26gt;j)


printf("greater %u %26gt; %u", i, j);


else


if(i==j)


printf("equal");


}








To avoid this ambiguity, tell the compiler explicitly how you want it to treat your numbers - as singed or unsigned, i.e.:





if ( (signed) i %26lt; (signed) j)





or





if ( (unsigned) i %26lt; (unsigned) j)
Reply:Sure,





//ur program enters the main method here...


void main()


{


//u assign 2 variables i and j


unsigned i=1;


signed j=-1;


//this asks if i is less than j which it is not so it goes to the else


if(i%26lt;j)


printf("less");


//this asks if i is greater than j which it is so it runs the code in the else block


else


if(i%26gt;j)


//this tells it to print greater


printf("greater");


else


//because the code went into the above else block it will not bother to check this if statement..


if(i==j)


printf("equal");


}
Reply:Means: if i is less than j, print less


if i is more than j, print more


if i is equal to j, print equal


If u need any more help, email me: pravin.singh71@yahoo.com





(was that your homework)
Reply:It goes something like this.....





if i is less than j, then print "Less" to the screen.


if i is more than j, then print "more" to the screen.


if they are equal, then print "equal" to the screen.





I hope I didn't just do your homework for you and no, you don't have my permission to copy and paste it, if you're gonna cheat at least type it in yourself.

easter cards

I want C codes for mind games using numbers?

#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


#include%26lt;string.h%26gt;


void main()


{


int i,j,n,t,l,g,sum=0 ;


int b[4][4]={


{1,3,5,7},


{9,11,13,15},


{17,19,21,23},


{25,27,29,31}


};


int m[3][4]={


{2,3,6,7},


{12,13,16,17},


{22,23,26,27},


};


int u[3][4]={


{4,5,6,7},


{14,15,16,17},


{24,25,26,27}


};


int y[3][2]={


{8,9},


{18,19},


{28,29}


};


int x[3][4]= {


{10,11,12,13},


{14,15,16,17},


{18,19,30,31},


};


int z[3][4]= {


{20,21,22,23},


{24,25,26,27},


{28,29,30,31},


};


char name[20];


clrscr();


printf("\n Welcome to mindgame");


printf("\n\n Enter your name:\t");


gets(name);


fflush(stdin);


The above arr of nos should be disp on the screen and a person is asked to think a no from tat arr and if the no is ther he should enter 1 else 0, the output should be the number in his mind!!


Plz suggest me some codes and tell me how to extend the logic till n numbers

I want C codes for mind games using numbers?
Basically when this game is played with a set of cards , the numbers on the cards are arranged in such a way that the first number on the top left hand corner of each card is a power of 2.





The reason for this that any positive integer can be expressed as a sum of the powers of 2. Thus 7 = 4 + 2 + 1 ; 37 = 32 + 4 + 1 ; 41 = 32 + 8 + 1 and so on.





Now , the remaining numbers have to be arranged on each card in such a way that they can be obtained by adding the powers of 2 on the cards in which they are present. For instance the number 15 can be obtained by adding 8 + 4 + 2 + 1. Since these are the powers of 2 which will figure on the top left hand corner of the first four cards , the number 15 has to appear in the first four cards , and in no other card.





Again , suppose we consider 41 ; since 41 = 32 + 8 + 1 , which are the powers of 2 appearing on the first , fourth and sixth cards , 41 should appear only in the first , fourth and sixth cards , not in any other.





basically it comes down to arranging all the numbers in the right cards ; in your program it will be the right arrays.


Functions in c programming please??????

make a program that will input xcoordinate,ycoordinate,quadrants and results by using function..





this is the given program but it had a problem.. it is lacking..





#include %26lt;stdio.h%26gt;


getXcoordinate()


getYcoordinate()


getQuadrant()


displayQuadrant()





main()


{


getXcoordinate();


getYcoordinate();


getQuadrant();


displayQuadrant();


}


getXcoordinate();


{


printf();


scanf();


return ();


}





getYcoordinate();


{


printf();


scanf();


return ();


}








what's the next???


by getting quadrants, we used


if(x%26gt;0%26amp;%26amp;y%26gt;0)


q=1;





after getting quadrants?? what will be the next???





the error when i compile this program is "getYcoordinate" there is something with it...











please help me

Functions in c programming please??????
Your prototypes at the top need semi-colons and then your function definitions below main, take off the semi colon at the beginning of each function. That should make a big difference.





If you have questions feel free to ask directly.
Reply:Hi,


Try this:


--------------------------------------...


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;





int getXcoordinate();


int getYcoordinate();


void getQuardant(int,int);





void main()


{


int x,y;





clrscr();


x=getXcoordinate();


y=getYcoordinate();


getQuardant(x,y);


getch();


}





int getXcoordinate()


{


int x;


printf("\nEnter X coordinate: ");


scanf("%d",%26amp;x);


return(x);


}





int getYcoordinate()


{


int y;


printf("\nEnter Y coordinate: ");


scanf("%d",%26amp;y);


return(y);


}





void getQuardant(int x,int y)


{


clrscr();


if(x%26gt;0 %26amp;%26amp; y%26gt;0) printf("Points (%d,%d) will lie in first quadrant.",x,y);





if(x%26lt;0 %26amp;%26amp; y%26gt;0) printf("Points (%d,%d) will lie in second quadrant.",x,y);





if(x%26lt;0 %26amp;%26amp; y%26lt;0) printf("Points (%d,%d) will lie in third quadrant.",x,y);





if(x%26gt;0 %26amp;%26amp; y%26lt;0) printf("Points (%d,%d) will lie in fourth quadrant.",x,y);


}


--------------------------------------...