C++ :: Elevate Input Number Without Using Advanced Functions
Oct 9, 2013
Last night I was trying to see if is possible to multiply one number to another without using any given function(like pow) or the multiply sign or whatever. Just using addition and subtract signs (+and -) with a for loop. It worked.
Now I am trying to understand if is possible to elevate one given number (inserted by the user) to another number without using again any function or the multiply / divide/ etc functions. Just add and subtract functions.
I have an advanced data namespace in which I hope to be able to read a data variable of any type and transfer it's bytes into another type of a multiple size.
i.e. char[4] -> int; int -> short[2]; short -> char[2]; char[2] -> short;
but I'm having some trouble, I get the following errors (because as a template it must compile from start)
What "int values" is supposed to mean as parameters to these functions? I'm not sure what do with them. Also how to input values into the array via functions. I was trying to but I just don't understand how to connect a user's input to a function to then enter into an array.
// input reads “values” integers from the user to place in the array data. It prompts the user for each value individually with the ordinal position of the value. void input (int data [size], int values); // Places the sum of corresponding values from arrays a and b and places the results in array s. The first “values” integers in the array are processed. void do_sums (int a [size], int b [size], int s [size], int values);
Write a function write with variable number of arguments that takes a string first argument followed by any number of arguments of type double and prints on the screen a string formatted by the rules described below. The first argument may contain formats in curly braces of the form {index[:specifier]}, where the square brackets show optional parts (this is :specifier may be missing), and index is the sequence number of an argument of type double (starting from sequence number 0).
Rules for formatting: In the printed string the curly brackets and their content will be replaced by the argument with the given index, formatted according to the given format specifier. If the format specifier is missing, the argument will be printed with its default format. For example:
write("The number {0} is greater than {1}.", 5, -3); will print The number 5 is greater than -3.
write("There are no format specifiers here."); will print There are no format specifiers here.
The format specifiers and their meanings are listed in the following table
Specifier MeaningFormat Output for 1.62 Output for 2.0 none default {0}1.62 2 ccurrency{0:c}$1.62 $2.00 escientific{0:e}1.620000e+000 2.000000e+000 ffixed point{0:f}1.620000 2.000000 iround to int{0:i}2 2
Limitations: You may limit the maximum number of arguments your function can process to a certain value, for example 10.
Suggested extensions: -Add an optional alignment specification in the format , e.g., make the format of the form {index[,alignment][:specifier]}, where alignment is an integer specifying the width of the field in which the corresponding argument will be printed. If alignment is positive, align to the right, if it is negative, align to the left. -Accept an optional integer after the specifier letter, specifying the required precision in the output. For example, {0:f2} will print the number 1.6234 as 1.62, but {0:f5} will print it as 1.62340.
I am wondering is there a way to make function A uses random number stream say starting from srand(1) and function B uses random number stream say starting from srand(2) in the same program.
I want to write a program that is going to turn on my webcam and then display whatever the webcam is pointing at on the app. But I am more interested in knowing how I can get a video recording as input from the webcam to one of the program functions, because I suppose that is what should happen.
The program is supposed to read in a string from the user and then output the number of each vowel that the string has. My first function is initializing the vectors, and the one that I'm having trouble with is the function used to read the string from the user and save it.
// FUNCTION PROTOTYPES GO HERE: void init_vectors(vector<char> & vowels, vector<int> & frequencies); string read_text(const string & prompt);
[Code] ....
And I'm getting the error:
freq.cpp: In function ‘std::string read_text(const std::string&)’: freq.cpp:74: error: no matching function for call to ‘getline(std::istream&, const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)’
I'm not too sure if I can't use the function getline here or if there is something wrong with the function prototype itself but I'm pretty sure there isn't an error there as it was given to me.
I am supposed to implement the member functions of class Person.
class Person { public: Person(); Person(string pname, int page); void get_name() const; void get_age() const;
[Code] ....
The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Here is my code:
// Program Title: Person function // Program Description: The program prompts the user for first and last name and age. // It then prints the output that was provided by the user.
#include<iostream> #include<string> using namespace std; class Person {
I'm having trouble finishing this program. What I'm trying to do is create a class for Rational numbers. Get input from user for denominator and numerator. Store the numbers without a sign, and for the sign store it separately in char. I'm not supposed to use accessor functions. The part that I can't seem to know how to do is use the info that was stored in the addUp function. This function need to add two rational numbers. i need to make this function general enough that it can handle two fractions, or a fraction and a whole number, or two whole numbers. What I already have here is readin function which reads in the numerator and denominator. setnumerator and setdenominator to assign positive values. setsign should get the sign of the fraction. Finally addUp should addUp which I explained before. I have some ideas about writing the tests, but can't seem to know how to implement it to the program. The main program is still empty but all I'm supposed to do there is call the class functions.
Code: #include <iostream> using namespace std; class Rational { private: int numerator, denominator; char sign;
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code: matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //This function checks if two matrices are equal int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix int * getRow(int **A, int i, int n);
//This function returns one column of a matrix int * getCol(int **A, int j, int n);
Summary: 6 companies have a product in 5 different warehouses. Each company is identified by a positive ID number and each warehouse is identified by a number (1 for the first, 2 for the second,…)
Object: the object of this assignment is to write a C++ program which asks the user to enter a company ID number, and the number of products in each of the warehouses. It then computes and prints the total number of products for that company
If the total number of product is less than 100, it prints the message “place a new order”
Input: for each company, its ID, and the number of products in each warehouse with appropriate prompt messages. Example: Enter company ID number: 101 Enter number of products in warehouse #1:30 Enter number of products in warehouse #2:60 Enter number of products in warehouse #3:0 Enter number of products in warehouse #4:5 Enter number of products in warehouse #5:27 The total for company 101 is:122
Output: for each company, the total number of product, and the message “place a new order” if the total number of product is less than 100.
Method: 1. Define global variable, int total_prod to hold the total number of products for a company 2. define the function void compute_total() that uses a loop to read the number of the products in all warehouses for one company, computer the total number of products and store it into the global variable total_prod. 3. Define the function void new_order() that determines if a new order need to be placed as follows: if the total number of products (in the global variable total_prod) is less than 100, it prints the message “place a new order” 4. Your function main does the following in a loop: - read a company ID number - call function compute_total() to read the number of the product in all warehouses for that company, and to compute their sum - print the total number of the product for that company with an appropriate message - call the function new_order() to determine if a new order need to be placed.
I have to write a program that will ask you to put in a number between 0 and 9 and multiply it by pi. If the number put in is between 0 and 9 then pi is multiplied but if it isnt between 0 or 9, it will say the number is not between 0 and 9 and asks you to put it in again and will repeat until a number between 0 and 9 is put in.
I have got the program working to the extent that it the number is between 0 and 9 it will multiply it by pi but if its not between 0 and 9 it will say the number is not between 0 and 9 and ask to put in a new number.
I can't work out how to get the program to repeat itself if the number entered isnt between 0 and 9.
There's this program I'm trying to code where the user gives input of 2 values.
Sample :-
Value of X: 1234 Value of Y: 234567 Reversed value X: 4321 Reversed value of Y: 765432 New value as Z: 1223344567 (each number was taken in both X and Y)
Now the thing is I could do the reverse, and I've been trying to find out how.
Here's my code,
#include <iostream> using namespace std; int main(){ int x=0, y=0; int reverse_x=0; int reverse_y=0; int z = 0; int l;
[Code]....
What I thought about that area where I've put a comment on, I was wondering if I code a loop for the amount of times x gets divided by 10 until it becomes 0 and store it in the value, same thing for Y, and add the value together to get Z. But if that's how it is then how I could use that concept here.
how to take binary number as an input, generate partial products by bit-wise multiplication and in last step to add all the partial products to generate final products".
There's this program I'm trying to code where the user gives input of 2 values.
Sample :- Value of X: 1234 Value of Y: 234567 Reversed value X: 4321 Reversed value of Y: 765432 New value as Z: 1223344567 (each number was taken in both X and Y) Now the thing is I could do the reverse, and I've been trying to find out how.
Here's my code,
#include <iostream> using namespace std; int main(){ int x=0, y=0; int reverse_x=0; int reverse_y=0; int z = 0; int l;
[Code]...
What I thought about that area where I've put a comment on, I was wondering if I code a loop for the amount of times x gets divided by 10 until it becomes 0 and store it in the value, same thing for Y, and add the value together to get Z. But if that's how it is I can't seem to really get that idea how I could use that concept here. I'm not really good with programming, fairly new at this.
"Every number has at most 500 digits" under input. 500? How am I supposed to store that? And what if someone multiplies 999 ...(500 times) * 999 ... (500 times) ? I seriously doubt that my computer can store that! Is that just some huge value used to scare people off or is there some sneaky trick that I am unaware of?
I am done with the program, but won't post it, Lets not ruin the fum for ohers...Does that 500 limit also applies to result of operation?So max length of an input number is ~22 digits ? But still, how do I store 500 digits? Array would be a lot of wastage of memory (though I do have 256MB available).
In my calculator, I am trying to make it so that you put enter 1 to add, 2 to subtract, 3 to multiply, and 4 to divide. I am facing the issue of making it so that you must enter a number, however instead of it being any number, it must be 1, 2, 3, or 4, and if it is not any of those numbers, you must re-enter the number. Here is a little snippet of my code:
Code:
printf("Please enter 1 to add, 2 to subtract, 3 to multiply, or 4 to multiply: "); while (scanf("%d", &input) != 1) { while (getchar() != ' '); printf("Invalid option. Please try again: ");
Basically i want to input a number for example 123456 and get back how many 7's are in the input number and also to print out that same number with stars in between like this *1*2*3*4*5*6*. Here is what i have so far:
#include <iostream> using namespace std; int countSeven(int x){ if(x == 7)return 1; int c = 0; while(x/10 > 9)c++; return countSeven(x/10)+1;
when i compile it does not do what i tell it it just tells me there are 0 7's in the input number no matter how many there really are...