C++ :: Function That Takes Int As Argument And Doubles It?
Jan 7, 2014Write a c++ function that takes int as an argument and doubles it.the function does not return a value.
View 5 RepliesWrite a c++ function that takes int as an argument and doubles it.the function does not return a value.
View 5 RepliesConsider I have a rectangle like this:
Point p1(100,100);
Point p2(300,200);
Graph_lib::Rectangle r(p1,p2);
And also I have a function that takes a rectangle and returns a Point of that, say, top-left corner of that rectangle. For example:
Point N(Graph_lib::Rectangle R1);
My question is that, first, how to send that Rectangle r(p1,p2) to the Point N(Graph_lib::Rectangle R1) function? And then, how to return a Point from that function?
My IDE is visual studio 2012.
In the project I'm currently working on I define a class that only inherits from a parent class and takes one argument.
Does this class need to be defined in the header or source file? I read different answers around the internet.
Or is it better to always split definition and logica, even for something like an operator?
Why the program continues to prompt me for a vaild number even when the correct number( a gpa) is entered. I'm not sure if the problem lies in the isDouble function or the getDouble?
*/ Determines whether the string holds a valid double. Checks if each character is digit and there is no more than 1 decimal point.
*/
bool isDouble (const string &str) {
int decimal = 0;
for (unsigned int i = 0; i < str.size(); ++i){
if ( str[i] == '.'){
decimal++;}
[Code] ....
how would one write a function that doubles every element of a 20×10 2-dimensional array? this is what i've got so far.
double arrary [i][j];
for(double i=0; i<20;++i) {
for (double j=0;j<10;++j) {
array[i][j] =
[Code]......
I'm trying to get correct answers from my exam to study for a midterm:
Write a function that returns the maximum value of an array of doubles.
Declaration:
double maximum(array[],int a_size);
So far I've done:
double maximum(array[], int a_size) {
double maxVal;
maxVal = array[0];
for(int i=1; i < size; i++) {
if(array[i] > max
maxVal=array[i]
}
return maxVal;
}
I just need any corrections.
Second, I need to write a function definition that merges two vectors containing integers, alternating elements from both vectors.
Prototype:
vector<int> merge(const vector<int> a, const vector<int> b);
This is my definition so far:
vector<int>merge(const vector<int> a, const vector<int> b){
vector<int> merged;
for(int i=0; i<a.size+b.size; i++) {
if(flag == true)
merged.push_back(a);
flag = false;
else
merged.push_back(b)
flag = true;}
I think I'm lost in the second part...
I keep getting this error in my code. I believe it is because to use pow(x,y) both x and y have to be double, but how do i put that into my formula under calculations?
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main() {
// Declaration section: Declaring all variables.
[Code] ....
I just want to call the function : outputboo(), but I dont know how
Code: /*Enthusiastic
Pessimism
desensitize
uniqueness
*/
#include <stdio.h>
#include <string.h>
struct Books{
char title[50];
char author[50];
[Code]...
Write a c function which takes a string(as a character array, null terminated) and reverses it. it doesn't print the reversed string -- it modifies the given string so that it is reversed.
Code:
void reverse(char array[]) {
char tmp;
int i, j;
for(i=0;i<strlen(array); i++){
for(j=strlen(array)-1;j>=0;j--) {
tmp = array[i];
array[i] = array[j];
array[j] = tmp;
} } }
how come this doesn't work?
I have to make a program that takes several numbers into an array into a function, and send them back into the main function multiplied by 1.13. This is what I have so far:
#include <iostream>
using namespace std;
void connell(int&n[20])
main() {
int num[20];
[Code] ...
I do not know how to get numbers from an array from a main program to a function.
I searched the web for error: C3867... and the discussions where murky or obscure.
My code excerpt is:
#pragma once
#include <windows.h>
#include <stdlib.h>
#include <process.h>
void PutUpfrmIO(void *);
namespace WordParsor {
[Code] .....
I get the generic message:
error C3867: 'WordParsor::Form1::PutUpfrmIO': function call missing argument list; use '&WordParsor::Form1::PutUpfrmIO' to create a pointer to memberc:userskingc++wordparsorwordparsorForm1.h... and the suggestion fix generate another error.
One person suggested the gcroot<> object wrapper... but I do not know how to modify/declair the function or its argument type.
We have to write a function named fibonacci that takes an int indicating the length of the series and then store it in an array of size 20 printing the sequence from largest to smallest. Here is the small bit i have so far:
#include <iostream>
#include <iomanip>
using namespace std;
void Fibonacci( int );
int main( ) {
[Code] ....
I just need something to get me on the right track.
I have a school project in which need to create a function that takes a File Object as a Reference Parameter. Supposedly, it should allow me to read the first piece of data from others separated by a space from a file. The later be able to continue reading from the next piece of data.
I know how to set things up to read from the data file, such as using
Code:
#include <iostream> , and
Code:
ifstream inputFileName;
inputFile.open ("nameOfFile");
I also understand how to pass a variable by reference to a function. But I simply cannot put the two items together!
I tried using
Code:
void getFileInput (ifstream &); //parameter
getFileInput ("nameOfFile") // call
void getFileInput (ifstream &dataFileName)
In the function I used
Code:
ifstream inputFile;
inputFile.open (dataFileName);
I'm just not getting an understanding of this concept!
Write a function that takes two input arguments and provides two separate results to the caller, one that is the result of multiplying the two arguments, the other the result of adding them.Since you can directly return only one value from a function, you'll need the second value to be returned through a pointer or reference parameter.
Code:
#include "Code.h"
#include <iostream>
#include <string>
double x, y;
[Code] ....
How do I Write a function that takes two linked list as input arguments these linked list contain numbers like this:
num1->3->5->2->NULL (assuming that number 1 is 352)
num2->4->3->9->1->NULL (assuming that number 2 is 4391)
The function should return 1 if num1 points to a linked list which represents a smaller number than the number pointed to by num2 linked list. Otherwise, it returns -1. If both linked list point to exactly the same number, returns a 0.
I am writing a program in which a Fucntion has to be wriiten to parse the Command Line . When I include Code for parsing in main fuction iteslf ,its run ok . But I want to make a fucntion of that code and call it from main ,than it show Segmentation error .
By using Debugging I found Some thing is mess with " -m" Parameter of Command line , But Cant Rectify it ..
Code:
int main (int argc, char *argv[]){
//get_parameter_value(argc,argv);
// buffer[packet_size+1]= char ("'");
while (argc > 1) {
if (argv[h][0] == '-')
[Code] .....
I was required to write a program that takes a baseball players statistics and displays there averages. I was required to make 3 function in the file to perform this tasks. my problem I am having a division problem in the SLG function. My compiler does not require the system ("PAUSE"); command.
OUTPUT
The player's batting average is: 0.347
The player's on-base percentage is: 0.375
The player's slugging percentage is:
(test)AB = 101
(test)Tot Base = 58
0.000
Code:
/* Batting Average Program
file: batavg1CPP.cpp
Glossary of abbreviations:
BA = batting average
PA = plate appearances
H = hits
BB = bases on balls (walks)
[Code] ....
How to pass an int that I got from user input into a function to use it. I am trying to print out the words to a string of numbers.
I got the input from user.
I got an absolute value of the input.
I then separate the string into individual digits and name them.
I can print these out.
Then I started my if statement by checking if the original input was zero, and if it is, printing zero and exiting.
Then I an trying to pass the digits into a switch function and this is where I go off the rails.
Code:
#include <iostream>
#include <string>
#include <cstdio>
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
[Code] .....
I was reading about void as function argument, but I did not fully understand it's meaning in C.
In C++
void foo(void) {}
and
void foo() {}
are the same. It means no arguments for foo function. But in C it's different. First function means the same as in C++, but second means
In C, an empty parameter list means that the number and type of the function arguments are unknown. But if it is unknown you can't use this arguments if user specifies same. Because here are no variables to store them. So doesn't result are the some? You do not get any arguments. O do I can get this arguments from some hidden variable?
For example.
void foo() {
printf("%d", var);
}
foo(5);
It is very unclear for me. Do this apply to main function too?
int main(void)
int main()
or can I use arguments given to int main() like given to int main(int argc, char* argv[])
I have two questions :
1)
#include <iostream>
using namespace std;
void func1() {
cout << "Func1" << endl;
[Code] ....
Why ptr_func1() does not work here?
IntelliSense: expression preceding parentheses of apparent call must have (pointer-to-) function type
2) How can i pass func1 to func2 as parameter?
I tried void func1(void* function), but I think I'm wrong here.
i have used single argument conversion function which gets called in below scenario.
Code:
#include<iostream>
using namespace std;
class Demo
{
[Code]....
It is giving error "test3.cpp: In function `int main()':test3.cpp:18: syntax error before numeric constant"
But it should work as Demo d=100; works
I get an error when i try to compile this code. I tried to allocate memory in main function and that works. But why it doesn't work in function? I think that there is something wrong with function argument, but not sure.
Code:
#include <iostream>
#include <fstream>
using namespace std;
struct Word
[Code].....
I have a function
Code:
int exec_program(char * arguments[])
{
...
}
I can call it like this without a problem:
Code: char * uselessvariable[] = {"/bin/echo", "Testing", NULL};exec_program(uselessvariable);
However I get an error if I try to compile it like this:
Code: exec_program({"/bin/echo", "Testing", NULL});
How, in c, I can put this array inside of the argument in one line without having to name a new variable name?
What does collection of parameters as argument of a function in C mean? Also any place I can refer to find those parameters?
Googling gives me Parameters and Arguments But not really sure whether that is what is needed.
I was wondering if one could write a function that could accept one or the other variable type.
Ex: I have 2 arrays, int** and double**, and a function
Code: void PGMWrite(double** Matrix, int Matrix_dimension){.....}
Is there any way to change the function to
Code: void PGMWrite(int** Matrix || double** Matrix, int Matrix_dimension){.....}
And then have some sort of type identifier in the function that picks the correct section via an if loop? If so how, and how would I identify in the function if the input it type double or int?
I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:
BASEBOARD_ERROR_KIND ZMP zrc :: :: :: Baseboard GetRS232Data (char * msg )
RS232 data acquisition.
Argument:
[Out] msg Address of the acquired data.
Returns:
BASE_OK RS232 data acquisition success
BASE_BASE_232_GETDATA_ERR RS232 data acquisition failure
I have trouble writing the relevant code in the main program that invokes this function. Here is a snippet of what I have tried:
# include "Baseboard.h"
int main () {
Baseboard _Baseboard; // Class name is Baseboard
char *msg ;
[Code] ......
The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?
Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value) .