Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Wow Tottaly Lost In C++
zach101
post Oct 25 2005, 09:43 PM
Post #1


Premium Member
********

Group: Members
Posts: 178
Joined: 26-June 05
Member No.: 8,699



Guys some of these exercises in my book i just tottaly dont get and this is basically a self taught class so im trying my best to learn just out of the book. But there are a few problems in paticular i need help with such as these


prob 1. should do/look like this

Enter a number: 140 // so the 140 is cin >> num;
Prime factors: 2,2,5,7 // so it displays the prime factors of the number


Now if some one could tell me how to do that i would be so appreciated some people said do a do while while others said a for loop basically im just tottaly lost any help would be awesome. Thanks guys
Go to the top of the page
 
+Quote Post
sangdukseo
post Oct 25 2005, 09:55 PM
Post #2


Member [Level 3]
******

Group: Members
Posts: 97
Joined: 16-June 05
Member No.: 8,302



do-while and for loops pretty much do the same thing so it doesn't matter which one you use. I think what they mean is that if you want to find the primes of 140 for example, the first time you run through your loop you would get say, 70 and 2. Within the loop, you have an if-then statement that says that if any prime #'s come up, store it within an array that uses your counter for your loop as it's location in the array. That stores 2. The next time the loop runs, 70 splits up into 35 and 2. The array stores 2 and the loop runs again. This continues until you only have primes. The loop ends, and you tell the program to cout << array and put the command inside a for loop using the same counter that you used for your loop above. I hope this helps. Maybe I should have just written out the program. But this is the logic involved.
Go to the top of the page
 
+Quote Post
zach101
post Oct 26 2005, 05:43 PM
Post #3


Premium Member
********

Group: Members
Posts: 178
Joined: 26-June 05
Member No.: 8,699



Okay im really sorry im buging you guys again but i seriously need help in this C++ thing cause im currently takign VB at the same time.... I guess i should of taken VB first eh? cause this c++ class is basically my intro into any kind of programming and its pretty hard for me so please bare with my newb self. Okay heres the code i have writen so far of course it has erros but i feel like im at least a little closer and thanks sang that reply did help but could u point out whats screwing the code up this time?

CODE
/* Exercise 12
Everman, Zach Oct 26 05 */

#include<iostream.h>

int main()

{
int num;
int divisor;

cout << "Enter a number";
cin >> num;
divisor=2;


while (divisor <= num) {
divisor = divisor + 1;
}
if (num / divisor != 0)
cout << divisor;
else
cout <<  " no Prime factors";

}
return(0);
}
Go to the top of the page
 
+Quote Post
mama_soap
post Nov 1 2005, 12:38 AM
Post #4


Super Member
*********

Group: Members
Posts: 282
Joined: 30-May 05
From: Bangalore
Member No.: 7,686



Ok, a couple of things.

First, you have:

CODE
while (divisor <= num) {
divisor = divisor + 1;
}


That's an extra closing bracket. When this code is compiled, the compiler will give the variable "divisor" some location in the memory and dump the number "2" there. Then it will excecute this while loop and by the time it is done, that location will have the value of "num", and the rest of the code is rendered pointless.

CODE

if (num / divisor != 0)
cout << divisor;
else
cout <<  " no Prime factors";


Okay, the num/divisor should be num%divisor. You only want to know if divisor divides evenly into num or not. For that, you only need to check on the remainder, not the quotient.

Also, this will output those divisors also which are non-primes. For instance, if num=8, the program will probably output 2,4,8, of which 4 and 8 are definitely composite. I'd suggest you write a small isprime() function and check if divisor is prime or not during every iteration. The code will look like this:

CODE
if(num % divisor != 0)
{
           if(isprime(divisor))
                               cout << divisor;
}


Again, the else statement should be outside the while loop, not inside it. If it's inside, your output will resemble:

CODE
2
no prime factors
4
no prime factors
no prime factors
no prime factors
8


which is clearly not desirable. Please remember to write out the function isprime() in full. It's a standard function you'll find in any text, and besides, it's also a good exercise if you're starting out smile.gif

Good luck and keep us updated.
Go to the top of the page
 
+Quote Post
sharpz
post Nov 16 2005, 11:34 PM
Post #5


Member [Level 1]
****

Group: Members
Posts: 67
Joined: 16-November 05
Member No.: 14,373



you could try factoring first. add a loop to check all integers beetween 1 and the number are evenly divisible. use the modulus operator ( % ) to check if there is a remainder or not when you divide. if you look on google, you can see many examples of prime numbers and factoring in C++ such as http://forums.dreamincode.net/showtopic13192.htm or
http://www-users.york.ac.uk/~ajm143/prime.shtml
Go to the top of the page
 
+Quote Post
baseballkid781
post Nov 22 2005, 02:33 AM
Post #6


Newbie
Group Icon

Group: Banned
Posts: 4
Joined: 21-November 05
Member No.: 14,693



QUOTE(zach101 @ Oct 25 2005, 05:43 PM)
Guys some of these exercises in my book i just tottaly dont get and this is basically a self taught class so im trying my best to learn just out of the book.  But there are a few problems in paticular i need help with such as these
prob 1.  should do/look like this

Enter a number: 140    // so the 140 is cin >> num;
Prime factors: 2,2,5,7  // so it displays the prime factors of the number
Now if some one could tell me how to do that i would be so appreciated some people said do a do while while others said a for loop basically im just tottaly lost any help would be awesome.  Thanks guys
*



The basics of C++

C-- is a name used for at least two different, unrelated programming languages. The goal of both languages is to bring the C programming language closer to computer hardware, creating more compact machine code while keeping C syntax, usage standards, and readability for ease of use by the many programmers already familiar with C.
The two known variants are:
C--, a compiler for a portable intermediate assembly language that serves as an interface between high-level compilers and retargetable, optimizing code generators.
Sphinx C--.

Programming Language-

A programming language or computer language is a standardized communication technique for expressing instructions to a computer. It is a set of syntactic and semantic rules used to define computer programs. A language enables a programmer to precisely specify (but see Genetic Programming) what data a computer will act upon, how these data will be stored/transmitted, and what actions will be taken under various circumstances.

Features of programming language

Each programming language can be thought of as a set of formal specifications concerning syntax, vocabulary, and meaning.
These specifications usually include:
Data Types
Data Structures
Instruction and Control Flow
Design Philosophy
Compilation and Interpretation
Most languages that are widely used, or have been used for a considerable period of time, have standardization bodies that meet regularly to create and publish formal definitions of the language, and discuss extending or supplementing the already extant definitions.
[edit]
Data types
Internally, all data in modern digital computers are stored simply as zeros or ones (binary). The data typically represent information in the real world such as names, bank accounts and measurements and so the low-level binary data are organized by programming languages into these high-level concepts.
The particular system by which data are organized in a program is the type system of the programming language; the design and study of type systems is known as type theory. Languages can be classified as statically typed languages, and dynamically typed languages. Statically typed languages can be further subdivided into languages with manifest types, where each variable and function declaration has its type explicitly declared, and type-inferred languages. It is possible to perform type inference on programs written in a dynamically typed language, but it is entirely possible to write programs in these languages that make type inference infeasible. Sometimes dynamically typed languages are called latently typed.
With statically typed languages, there usually are pre-defined types for individual pieces of data (such as numbers within a certain range, strings of letters, etc.), and programmatically named values (variables) can have only one fixed type, and allow only certain operations: numbers cannot change into names and vice versa. Most mainstream statically typed languages, such as C, C++, C#, Java and Delphi, require all types to be specified explicitly; advocates argue that this makes the program easier to understand, detractors object to the verbosity it produces. Type inference is a mechanism whereby the type specifications can often be omitted completely, if it is possible for the compiler to infer the types of values from the contexts in which they are used -- for example, if a variable is assigned the value 1, a type-inferring compiler does not need to be told explicitly that the variable is an integer. There are however many different uses for integers; it might e.g. make sense in a program to prevent inadvertent adding of a phone number to the number of apples in a box. Therefore some languages such as Ada allow defining different kinds of incompatible integers; this is called strong typing. Type-inferred languages can be more flexible to use, particularly when they also implement parametric polymorphism. Examples of type-inferring languages are Haskell, MUMPS and ML.
Dynamically typed languages treat all data locations interchangeably, so inappropriate operations (like adding names, or sorting numbers alphabetically) will not cause errors until run-time -- although some implementations provide some form of static checking for obvious errors. Examples of these languages are APL, Objective-C, Lisp, Smalltalk, JavaScript, Tcl, Prolog, Python, and Ruby.
Strongly typed languages do not permit the usage of values as different types; they are rigorous about detecting incorrect type usage, either at runtime for dynamically typed languages, or at compile time for statically typed languages. Ada, Java, ML, and Oberon are examples of strongly typed languages.
Weakly typed languages do not strictly enforce type rules or have an explicit type-violation mechanism, often allowing for undefined behavior, segmentation violations, or other unsafe behavior if types are assigned incorrectly. C, assembly language, C++, and Tcl are examples of weakly typed languages.
Note that strong vs. weak is a continuum; Java is a strongly typed language relative to C, but is weakly typed relative to ML. Use of these terms is often a matter of perspective, much in the way that an assembly language programmer would consider C to be a high-level language while a Java programmer would consider C to be a low-level language.
Note that strong and static are orthogonal concepts. Java is a strongly, statically typed language. C is a weakly, statically typed language. Python is a strongly, dynamically typed language. Tcl is a weakly, dynamically typed language. But beware that some people incorrectly use the term strongly typed to mean strongly, statically typed, or, even more confusingly, to mean simply statically typed--in the latter usage, C would be called strongly typed, despite the fact that C doesn't catch that many type errors and that it's both trivial and common to defeat its type system (even accidentally).
Aside from when and how the correspondence between expressions and types is determined, there's also the crucial question of what types the language defines at all, and what types it allows as the values of expressions (expressed values) and as named values (denoted values). Low-level languages like C typically allow programs to name memory locations, regions of memory, and compile-time constants, while allowing expressions to return values that fit into machine registers; ANSI C extended this by allowing expressions to return struct values as well (see record). Functional languages often restrict names to denoting run-time computed values directly, instead of naming memory locations where values may be stored, and in some cases refuse to allow the value denoted by a name to be modified at all. Languages that use garbage collection are free to allow arbitrarily complex data structures as both expressed and denoted values.
Finally, in some languages, procedures are allowed only as denoted values (they cannot be returned by expressions or bound to new names); in others, they can be passed as parameters to routines, but cannot otherwise be bound to new names; in others, they are as freely usable as any expressed value, but new ones cannot be created at run-time; and in still others, they are first-class values that can be created at run-time.
[edit]
Data structures
Most languages also provide ways to assemble complex data structures from built-in types and to associate names with these new combined types (using arrays, lists, stacks, files).
Object oriented languages allow the programmer to define data-types called "Objects" which have their own intrinsic functions and variables (called methods and attributes respectively). A program containing objects allows the objects to operate as independent but interacting sub-programs: this interaction can be designed at coding time to model or simulate real-life interacting objects. This is a very useful, and intuitive, functionality. Languages such as Python and Ruby have developed as OO (Object oriented) languages. They are comparatively easy to learn and to use, and are gaining popularity in professional programming circles, as well as being accessible to non-professionals. It is commonly thought that object-orientation makes languages more intuitive, increasing the public availability and power of customized computer applications.
[edit]
Instruction and control flow
Once data has been specified, the machine must be instructed how to perform operations on the data. Elementary statements may be specified using keywords or may be indicated using some well-defined grammatical structure.
Each language takes units of these well-behaved statements and combines them using some ordering system. Depending on the language, differing methods of grouping these elementary statements exist. This allows one to write programs that are able to cover a variety of input, instead of being limited to a small number of cases. Furthermore, beyond the data manipulation instructions, other typical instructions in a language are those used for control flow (branches, definitions by cases, loops, backtracking, functional composition).
[edit]
Design philosophy
For the above-mentioned purposes, each language has been developed using a special design or philosophy. Some aspect or another is particularly stressed by the way the language uses data structures, or by which its special notation encourages certain ways of solving problems or expressing their structure.
Since programming languages are artificial languages, they require a high degree of discipline to accurately specify which operations are desired. Programming languages are not error tolerant; however, the burden of recognizing and using the special vocabulary is reduced by help messages generated by the programming language implementation. There are a few languages which offer a high degree of freedom in allowing self-modification in which a program re-writes parts of itself to handle new cases. Typically, only machine language, Prolog, PostScript, and the members of the Lisp family (Common Lisp, Scheme) provide this capability. In MUMPS language this technique is called dynamic recompilation; emulators and other virtual machines exploit this technique for greater performance.
[edit]
Compilation and interpretation
There are, broadly, two approaches to execute a program written in a given language. These approaches are known as compilation, done by a program known as a compiler; and interpretation, done by an interpreter. Some programming language implementations support both interpretation and compilation.
An interpreter parses a computer program and executes it directly. One can imagine this as following the instructions of the program line-by-line. In contrast, a compiler translates the program into machine code -- the native instructions understood by the computer's processor. The compiled program can then be run by itself.
Compiled programs usually run faster than interpreted ones, because the overhead of understanding and translating the programming language syntax has already been done. However, interpreters are frequently easier to write than compilers, and can more easily support interactive debugging of a program.
[edit]
History of programming languages

The development of programming languages, unsurprisingly, follows closely the development of the physical and electronic processes used in today's computers.
Programming languages have been under development for years and will remain so for many years to come. They got their start with a list of steps to wire a computer to perform a task. These steps eventually found their way into software and began to acquire newer and better features. The first major languages were characterized by the simple fact that they were intended for one purpose and one purpose only, while the languages of today are differentiated by the way they are programmed in, as they can be used for almost any purpose. And perhaps the languages of tomorrow will be more natural with the invention of quantum and biological computers.
Charles Babbage is often credited with designing the first computer-like machines, which had several programs written for them (in the equivalent of assembly language) by Ada Lovelace.
In the 1940s the first recognizably modern, electrically powered computers were created. Some military calculation needs were a driving force in early computer development, such as encryption, decryption, trajectory calculation and massive number crunching needed in the development of atomic bombs. At that time, computers were extremely large, slow and expensive: advances in electronic technology in the post-war years led to the construction of more practical electronic computers. At that time only Konrad Zuse imagined the use of a programming language (developed eventually as Plankalkül) like those of today for solving problems.
Subsequent breakthroughs in electronic technology (transistors, integrated circuits, and chips) drove the development of increasingly reliable and more usable computers. The first widely used high level programming language was Fortran, developed during 1954–57 by an IBM team led by John W. Backus. It is still widely used for numerical work, with the latest international standard released in 2004. A Computer Languages History graphic shows a timeline from Fortran in 1954.
Dennis Ritchie and Brian Kernighan developed the C programming language, initially for DEC PDP-11 in 1970. Later with lead of Bjarne Stroustrup the programming language C++ appeared in 1985 as an Object oriented language vertically compatible with C. Sun Microsystems released Java in 1995 which became very popular as an introductory programming language taught in universities. Microsoft presented the C# programming language in 2001 which is very similar to C++ and Java. There are many, many other languages (cf. List of programming languages).

Notice from BuffaloHELP:
Pure garbage post! OpaQue was lenient on your warning. Source http://en.wikipedia.org/wiki/C-- and not a single respect for our board rules! DO NOT COPY AND PASTE FROM ANOTHER SOURCE, DO NOT PLAGIARIZE! Disabled from posting indefinitely.


This post has been edited by BuffaloHELP: Nov 22 2005, 10:51 PM
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. My Real Story(6)
  2. Lost The T V Series(3)
  3. Connect To Remote Oracle Database With Toad(7)
  4. Fable: The Lost Chapters(11)
  5. Lost In An Indian City(7)
  6. Cannot Delete Folder After Lost Ftp Connection(7)
  7. Lost Wife Joke(1)
  8. Computer Admin Login With Lost Password?(15)
  9. Balttlefield(4)
  10. Capcom's Lost Planet(1)
  11. The Lost Dr Seuss Poem(4)
  12. Lost Information Caused Unable To Contact The Billing Department(1)
  13. What Would You Do If You Got Lost In An Unfamiliar City?(8)
  14. Mac Classic Lost Password(4)
  15. Wanna Learn Html From Scratch(5)
  1. Boy Survives Six Hours At Night Lost In Dead Sea(5)
  2. Computinghost Has Lost My Web Site. [resolved](5)
  3. Horrible, Horrible Case(10)
  4. Lost Odyssey(2)
  5. Why Smf Has Lost Its "touch"(4)
  6. Finding The Way(1)
  7. Our Country Has Lost It(4)
  8. Lost Power City(2)
  9. Help Me, Lost Skill Suddenly(2)
  10. Do You Believe:losing Weight By Eating At Mcdonald's!(11)
  11. Golden Sun: The Lost Age(1)
  12. How Do Uninstall Deep Freezer If I Lost Password?..(2)
  13. How I Lost 60lbs In 4 Months(1)