C++ :: How To Implement Symbolic Constants To Factor Out If Else Statements
Feb 17, 2014
My assignment is to create a simple stock broker program that ask the user how much they are willing to invest and ask what company they would like to invest in. Finally it outputs how many shares the user will have based on their investment amount. My code is below. My professor said to declare symbolic constants and factor out the if else statements. Ive been struggling trying to understand constant variables. How do I use const variables to factor out the if else statements?
"A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value. You must initialize a constant when it is created. C++ has two types of constants: literal and symbolic.
A literal constant is a value typed directly into your program wherever it is needed. For example, consider the following statement:
long width = 5
This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can't assign a value to 5, and its value can't be changed.
The values true and false, which are stored in bool variables, also are literal constants.
A symbolic constant is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. Here's a statement that sets the point reward for killing a zombie:
const int KILL_BONUS = 5000;
Whenever a zombie is dispatched, the player's score is increased by the reward:
playerScore = playerScore + KILL_BONUS;
If you decide later to increase the reward to 10,000 points, you can change the constant KILL_BONUS and it will be reflected throughout the program. If you were to use the literal constant 5000 instead, it would be more difficult to find all the places it is used and change the value. This reduces the potential for error."
what's the difference? Here is a program to demonstrate what I'm having trouble conceptualizing.
#include <iostream> using namespace std; int main() { int width = 10, length = 10; int area = width * length; cout << "Width: " << width << endl; cout << "Length: " << length << endl; cout << "Area: " << area << endl; return 0; }
Now, why would it be harder to go in and changed a regularly defined integer than one defined with the 'const' keyword proceeding it? For example, the width and length variables. My confusion comes from the point that they seem to both simply be variables with a value assigned to them. I feel as if the process of having to change a literal constant's value is synonymous to the process of having to change a symbolic constant's.
****************************************************************** CSCI 240 Program 5 Part 2 Spring 2013
Programmer: Section: 1 Date Due: 3/1/13
Purpose: This program uses functions to calculate the surface area of various shapes. It is an exercise in learning to write functions. ******************************************************************/
#include <iostream> #include <iomanip> using namespace std; #define PI 3.14 //Symbolic constant for the value of PI
int Menu(); int getValue( string prompt, int lowerBound, int upperBound );
So I'm doing this problem where the user will input a number that will be factored until the number is 1, 2 people will play this game and they will take turns entering a number that the initial number(named gameN) will be divided by until it is 1. Whoever get it to 1 wins. My problem is I'm not really sure how to do this, I'm pretty new to programming so besides some if statements and a loop, the problem shouldn't require too much to write. Some rules for the game are, the number entered to factor the gameN cant be less than 2, and has to divide evenly into gameN, which I have signifies with modulus. Here is where I'm at so far.
#include <stdio.h> int main(){ int gameN; int p1f; int p2f; printf("What number should the game be played with?"); scanf("%d", &gameN);
Write a C-program that efficiently multiplies a number by a factor 2 to the power n. The number to multiply and n are variables, which get a value at the start of the program.
Clue: 1 shift to the left is the same as multiplying by 2. 2 shifts to the left are the same as multiplying by 4. 3 shifts to the left are the same as multiplying by 8
How can I calculate GCF of many numbers? I thought I could calculate two by two numbers, but it not seems to be a very effective idea. There is my function:
int gcf (unsigned int x, unsigned int y) { return (y == 0) ? x : gcf (y, x % y); }
i want to write a program which find the biggest prime factor of a number for example the biggest prime factor of six is three or the biggest prime factor of fifteen is five. What is my program bug
Code:
#include <stdio.h> // main functions #include <math.h> // for sqrt function int main() { int i, j, k, f; // F = Flag; printf("Enter K
I am trying to find the largest prime factor of a number. But when I am trying to determine if a number is a prime number in the function:
int is_prime(int number), I am unable to exit the loop.
Here is the code:
#include<iostream> using namespace std; int is_prime(int number) //the problem is in this function { int num = number; int factor=0; do{ num++; for(int i=1;i<(num+1);i++){
[code].....
So when the program runs, it first divides 20 by 2, to get 10, then divides 10 by 2 to get 5. Since, // condition 1 is not met, it passes 2 to the function int is_prime(int number). The function is able to return 3, but cannot exit the loop when num is 4.
I think the problem lies in the function: int is_prime(int number).
I am working on a code that is suppose to get vowels and consnants from a string. So far i got up to trying to get the vowels from a string. this is what i have so far:
#include <iostream> #include <string> // includes go into header using namespace std; int getword(string word); int getvowels(string word);
I need codes for a program in C or C++ that will show the real factor (the smallest one, without the number 1) when an integer is given as input.
When the program is started, it will ask to enter a number and press enter. After entering a number, it will first check if it is a prime number or not. If prime, it will notice that it is a prime number, otherwise, it will print the smallest real factor of the given integer.
For example, if 12 is entered, it will print, the smallest real factor for this number is: 2
If 27 is entered, it will print, the smallest real factor for this number is: 3
I need to write a program in which you do the following:
Define three named constants using the appropriate data types: DEC_NUM = 65; HEX_NUM = 0x7a; LETTER = 'f';
Then display each of these constants in decimal, in hexadecimal, and as a character using cout. Your program will have a total of nine cout statements.
why strcmp() doesn't return true when comparing a string constant with a string that was acquired via a linked list. By the way, the data in the linked list was taken from a text file. Does that imply that there's a new line () character in the string from the linked list?
I'm incrementing the pointer to buffer 150 bytes beyond its reserved 50. I see testwalk, followed by bracketout, followed by bracketin printed by the overflow on buffer.
The memory locations are ordered descending from their call order. Why is this the case?
One would think that they would be written in ascending order as I call them. I can only assume that they're compiled bottom up - where could I read about this process?
My problem is that some of my conditions are not being triggered when the price level of a medium or large discount is smaller than the cost of the widget(defined as .40) An example being ,base price, $2 med discount 80%, and large discount 90%. My med discount is,1.6(80%) off of 2, so $.40, which is fine, but 90% off mean 1.8(90%) off of a base of $2, which is $.20 which is too low., and should trigger the 4th if statement, Nick you are discounting large purchases too much!. However when I run this in the program, I am triggering the first if statement.
#include <math.h> #include <stdio.h> #define widget_cost 0.40 int main (){ float sellprice; float discount_med_price;
I've just started learning the C language, and I'm stuck on something that is probably quite simple.how to get IF statements working within WHILE loops. My code is below. I am trying to write a program to count the number of words in a sentence, and obviously stop counting after a full stop has been entered. I created a variable called 'spaces' which will increase by one after the user enters a space. However, when the IF statement is in place, the loop never terminates, even if I enter a full stop. When I delete the IF statement, the loop functions correctly.
Code:
#include <stdio.h> int main() { char x; char a; char y; int spaces = 0; }
if (letter3==" "){ letter3==letter; PrintCharacterLineEnd();
This is a code. the string "letter3" contains the string " ". What I want to happen is when, letter3 contains the text " " I want to go to the function PrintCharacterLineEnd.
The problem with this code is that there are no errors showing, but when i compile the program, the if else statements do not carry out as they should do. Both if statements show the same answer, for example the second if statement. If I type Y or N then I get the same thing, 'You will now choose an event'. How do i get rid of this problem? Is char supposed to be used or string?
im a newbie C user and im having a little trouble in these for loop of mine im using. the first iteration is all fine but on the second and succesive iterations the first gets statement is skipped. im making a program that would ask the user to input multiple informations for atleast 5 people. i was also asked to use structures.. here is the code i have come up so far.. ive been stuck in it for like 3 hours now.
I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."
However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??