Monday, May 24, 2010

What would this look like in C++ code?

Write a program that reads ten numbers (in an array), and counts the number of elements in the array that are divisible by 3.

What would this look like in C++ code?
#include %26lt;iostream%26gt;


using namespace std;


int main( int, char **)


{


int i, ary[10];


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


cin %26gt;%26gt; ary[0];


int cnt = 0;


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


{


if (ary[i] % 3 == 0)


++cnt;


}


cout %26lt;%26lt; cnt %26lt;%26lt; endl;


return 0;


}
Reply:all numbers are divisible by 3. No need to load them into an array.
Reply:It could look very similar to a program written in C to do the samething. Shouldn't you do your own homework? At lease give it a try and ask for help.





Actually - I've seen someone earlier posting a nearly complete program to do the same thing but having problems withit.

chrysanthemum

Explain line by line the following C++ code?

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


#include %26lt;conio.h%26gt; //for getch()


#include %26lt;ctype.h%26gt; //for isdigit


#include %26lt;stdlib.h%26gt; //for atoi





main()








{


char ch;


char entier[6];


int compteur=0;


cout %26lt;%26lt; "Enter a whole number : ";


cout.flush ();


do





{


ch = getch();


if ( ch=='\b' %26amp;%26amp; compteur%26gt;0)








{


compteur--;


cout %26lt;%26lt;'\b';


cout %26lt;%26lt; ' ';


cout %26lt;%26lt;'\b';


cout.flush ();


}


else


if (compteur%26lt;6 %26amp;%26amp; isdigit(ch))








{


entier[compteur++]=ch;


cout%26lt;%26lt;ch;


cout.flush();


}


}


while(ch!='\r');


entier [compteur]='\0';


int valeur=atoi(entier);


cout %26lt;%26lt; "\nThe whole number is " %26lt;%26lt; valeur %26lt;%26lt; endl;








system("pause");


}

Explain line by line the following C++ code?
Hey that's not a very good question to ask here. We are here to help you, not serve you.


So instead of giving a junk code and ordering others to explain the damn code, point to the specific line and ask to explain that line. Hope you got my point.
Reply:Damn, that code is just hard enough to read that I couldn't be bothered trying to comprehend it. Damn Yahoo! answers for not keeping indentation.
Reply:hey are you reading Programming for Dummies? Because I'm reading it too


What owuld this lookd like in C++ code?

A program that reads ten numbers (in an array) and displays them in reverse order in which they are read.

What owuld this lookd like in C++ code?
#include %26lt;iostream%26gt;


using namespace std;


int main( int, char **)


{


int i, ary[10];


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


cin %26gt;%26gt; ary[i];


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


cout %26lt;%26lt; ary[ 9 - i ];


cout %26lt;%26lt; endl;


return 0;


}
Reply:It could look very similar to a program written in C to do the samething. Shouldn't you do your own homework? At lease give it a try and ask for help.


How can you write and run C code on mac?

You get yourself a compiler that runs on a mac.





I am told you can get this to run on a mac:


http://www.leonardo-vm.org/





Apple itself distributes Xcode:


http://developer.apple.com/tools/downloa...





I checked out this Mac forum:





http://forums.macrumors.com/showthread.p...





Their response in the end is the Gnu GCC compiler which is so familiar to those of us who work on Linux.





http://gcc.gnu.org/

How can you write and run C code on mac?
you cant... its a mac.
Reply:C is portable and will run where it was compiled so long as you are not counting on OS specific API's





You can can GCC for this.
Reply:Find a compiler that works on the mac and get it.
Reply:Just get a compiler.
Reply:Get XCode:


http://developer.apple.com/tools/downloa...





Or just get GCC for Mac.





In order to download XCode, you'll have to create developer's account with Apple, which is free. XCode includes GCC.


Is there software that will write C++ code automatically?

It depends. There are packages out there that will convert UML to C++ code or many other types of code such as Java or SQL that you then run through a compiler or interpreter. Can it do that for a PC application? Probably no. Can it do that for general applications? Probably yes. Is it a good idea? That is a loaded question. It is definitely a programming paradigm shift, modifications and maintenance may be more difficult but on the other hand good requirements analysis and documentation are required since they "are" the code. Google "code generation" or "rose realtime" as a start.

Is there software that will write C++ code automatically?
Depends what you mean. If you mean, you don't have to touch any code, period, the short answer is no.





There are a number of IDEs (integrated development environments) that allow you to visually drag and drop elements into a project, and then specify certain parameters via a form; that minimizes your coding.





Borland's C++ Developer does this:





http://www.borland.com/us/products/cbuil...





So does Microsoft's Visual C++ Express:





http://msdn.microsoft.com/vstudio/expres...





And that tool is free to download / use.





There are also several programs that will convert one programming language to another; for example, a Visual Basic program to C++.

daffodil

I need help to write this C++ code urgently. I don't know where to start??

A.) Write a function named powfun () that raises an integer number passed to it to a positive integer power and displays the result. The positive integer should be the second value passed th the function. Declare the variable used to store the result as a long-integer data type to ensure sufficient storage for the result.





B.) Include the function written in (A.) in a working program. Make sure your function is called from main ().


Test the function by passing various data to it.

I need help to write this C++ code urgently. I don't know where to start??
just first try to learn c++ then code otherwise u will start hating it.
Reply:unsigned long powfun(int base, int power)


{


unsigned long res = 1;


for (int i=power; i%26gt;=1; --i)


res = res * base;





return res;


}





Now, do the main() function yourself.
Reply:A) create a function that returns long-integer


make sure the function has 2 parameters one for the base and one for the power.. example call power_func(base, power); once u have the skeleton done, write a loop the decreases Power until it is one, every time it decreases the power it should multiply the base time its self and store that into a temp. variable, then return That temp variable outside ur loop, after it


also u have to account for the 0th power which is always 1, u might want to block ur while with an if statement





Thats a normal way for doing powering , there are way faster algorithms out there for fast powering, like interval halving
Reply:#include %26lt;stdio.h%26gt;


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


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





int main()


{


int integer,


power,


result;





printf("Please input the integer\n");


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





printf("Please input the power\n");


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





{


result=pow(integer,power);


}





printf("%d raised to the %d power is %d",integer,power,result);





system("PAUSE");


return 0;


}


Is it possible to copy your c++ code and put into a word file? If so, how is it done?

C++ or any code prior to compilation or interpretation is a text file fun through a parser, etc. All you need to do is either cut-n-paste, as above, or open the file in Word, or whatever editor.

Is it possible to copy your c++ code and put into a word file? If so, how is it done?
Yea, you copy the code from your compilers editor and then paste it into word or elsewhere.