i'm trying to determine if a number from a file is a prime number using pointers first I wrote a function that determines if the number is a multiple of 7, 11, or 13. Then i wrote a function to see if the number is odd or even. Are they correct? Later i will print the results on screen but i'm extremely confused with pointers and i'm not sure how to write this prime function...
Code:
void divisible(int *n, int *result) { if (*n % 7 == 0 || n % 13 == 0 || n % 13 == 0) { *result = 1;
I'm wondering about the point of pointers to functions. When is it used?I saw the below example. It doesn't make sense to me. I mean we can easily write code that does the same without having to use pointers.
Code:
#include <stdio.h> int addInt(int a, int b); // Adds 2 integers int add5to4(int (*function_pointer)(int, int)); int main(void) { int sum; int (*function_pointer)(int, int); }
I am trying to use pointers to arrays in my function.
I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.jwhittle58, on 25 February 2015 - 06:06 PM, said:
I am trying to use pointers to arrays in my function. I can get the pointers to work outside of a function but I just can't figure out how to make them work in my function.
I'm given a mathematical function F(x) = etc..., the user inputs an initial x point and a final x point. The program finds the integration.
Below is a snippet of code.
/*Typedefs as given by prof*/ typedef double (*mainFunction) (double); typedef double (*calcArea) (mainFunction, double, double); int main () { answer = calcIntegral(mainFunction *curve1, calcArea *calcAreaRect, a, B)/>; /*it doesn't like this line*/ printf("The integral from %lf to %lf is: %.4lf", &a,&b,&answer); return 0; }
curve1 is a function that accepts a double and returns a double, calcAreaRect takes mainFunction main(which is F(x) so i stored the fn in curve1), double and double.
I'm trying to do is write a program that fits to a separate test program. The test program provides different size vectors that my function should try and binary search. If the element is found, the function should return 1, and if the element is not found, it returns -1.
Here is the code:
int binSearch(const vector<double> & data, int elem, int & comps) { { int beg=data[0]; int end=data[data.size()-1]; int mid=(end+beg)/2;
[Code] ......
The problem is that one of the vectors my function is supposed to binary search is a vector of size 0. I tried to throw in an if statement that would return -1 if the size was == 0, but then the program never fully completed and just kept running. So, how can I account for a size 0 vector in my function?
void foo(const double va, const int q) { int qaa[q]; ...... return; }
However, the compiler indicates allocator cannot allocate an array of constant size 0... how can I use the argument "q" to fix the size of array "qaa"?
I basically have some code that lets users register callbacks into a callback table at a specified index. There is one element in this table for each event that can trigger a callback. I basically do something like this:
How would I pass let say 2 array pointers to a function X , allocate memory for them in X , fill them with values and get them back in my main function without creating a structure.
example:
Code:
void X(int *a, int*b){ a= malloc ... b = malloc ... // fill a and b return them back to the main function } void main(){
The following function uses reference variables as parameters. Rewrite the function so it uses pointers instead of reference variables, and then demonstrate the function in a complete program.
int doSomething(int &x, int &y) { int temp =x; x = y * 10; y = temp * 10; return x + y; }
I understand how to covert the reference variables to pointers, however I am stuck on this error. Either I get the error listed in the title or (with a few changes) the error "invalid conversion from 'int' to 'int*'"
What am I doing incorrectly?
#include <iostream> using namespace std;
int doSomething(int*, int*);
int main(){ int X, Y, result;
[Code] ....
I have multiplied both x and y by 10 and then added them together!
Here is the result " //I really didn't know how else to use the "doSomething" function in a meaningful way. So... I just stated what the function does.
<< result << ". "; system("PAUSE"); return 0; } int doSomthing(int *x, int *y)
two parameters, CImage *pImgSrc and CImage *pImgDst. I think they are class pointers and the function is passed by reference. What should I learn to understand this function and its parameters? How should I use this function? how to use the function with two parameters CImage *pImgSrc and CImage *pImgDst.
I am in a position to choose between function pointers and subclassed objects. To make it clear, say I have to notify some object of some action (a timer for example); refer to the following two choices (a very basic code for demo purposes):
Version 1
typedef void TimerCallback(void *args); class Timer{ public: Timer();
[Code] .....
Version 2:
class TimerTask{ public: TimerTask(); virtual ~TimerTask(); void timedout()=0;
[Code] .....
which one is the standard C++ way and which one is efficient?
I'm having issues with pointers and relationship operators in C.
I need to find a max and min value in a void function using pointers. max and min would work if they had values. mul works, because you can just do math operations with pointers.
There are 0 errors and warnings; but max and min are never going to work as is.
Clearly I'm missing something.
#include <stdio.h> #include <stdlib.h> void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result); int main()
[Code]...
Your job will be to create a program that uses pointers. Your output must be done in the main function and the calculations MUST be done in the three functions. Therefore you MUST use pointers correctly.
You must declare and implement the following 3 functions. Below are the three prototypes that you must use in this program.
void max(int *a, int *b, int *c, int *d, int *result); void min(int *a, int *b, int *c, int *d, int *result); void mul(int *a, int *b, int *c, int *d, int *result);
The functions have the following meaning:
max finds the max value of a,b,c,d and stores the largest value in result. min finds the min value of a,b,c,d and stores the largest value in result. mul multiplies a * b * c and divides by d. Stores that value in result.
Below is an example input/output. This input will be read in via the keyboard (use scanf).
input output (note that user input is shown in bold) 1 2 3 4 Enter the 4 numbers: 1 2 3 4 The max is 4. The min is 1. (a * b * c) / d = 1 100 3 201 103 Enter the 4 numbers: 100 3 201 103 The max is 201. The min is 3. (a * b * c) / d = 585
Your output MUST match exactly the output below for the input from above. Your program must compile, failure to do so will result in 0 points. */
write a program which contains two global variables:
1.Account number (integer) 2.Account Balance (float)
and three functions :
A function called set values which sets initial values for account number and account balance,
A function called set values which prompt user to enter the values of the above variables,
A function called input transaction which reads a character value for transaction type that is D (for deposit) and W (for withdrawal ) and a floating point value for transaction amount which updates the account balance .
Implement a pointer to call each of the functions using C++.
How to find the size of an array in called function? When we pass the array a argument to function definition we will be having base address of array, so my understanding is that we will not get the size of an array? but is there any hacking for this to find size of array other than passing as size an argument to this called function?