C :: Create Function That Is Then Called To Calculate Powers
Nov 11, 2014
As I am taking my first steps in C, I study K&R (I guess most of you did the same, right?)
In the introductory chapter about functions (1.7) there is an example showing how to create a function that is then called to calculate powers (b**n). I simplified it to calculate only one given power, 2**5:
Code:
#include <stdio.h>
int power(int m, int n);
main() {
printf("%d", power(2,5));
[Code]....
It will then be called to make the calculation in the above string.
First things first: we already know how to use while(getchar!=EOF) to count characters (K&R chapter 1.5.2) but what if -instead- the input is a specific string? How to "read" it and how to tell my program that the string is finished? And most important: "reading" will be done in the function or in the rest of the body?
Ok so I am working on a game and I'm in the process of developing my Player class. Anyways, what I have is a keyboard function that is called in my main function to make a shape move.
void myKeyboardFunction(unsigned char key, int x, int y) { switch ( key ) {
[Code].....
But when I try to call it, trying to copy my previous method,
glutKeyboardFunc(Player1.playerControls);
I get an error
error C3867: 'Player::playerControls': function call missing argument list; use '&Player::playerControls' to create a pointer to member
I get an error saying it can't convert parameters. I would just like to understand why the arguments become a problem when I make the function a member of my class, when the first method I used is so easy.
I am working on this project where I need a function to be called every second. At this time, I am thinking that I have to create a thread but I am clueless on how it will get called every second.
When I call printStuff, the DerivedClass's function gets called. Now, if I remove the const part from the DerivedClass's printStuff function, we call the BaseClass's printStuff function.
There is already a thread with exactly the same problem I have, but the answer to solve the problem isn't stated at the end. Problem with callback as classmember.
So on lines 36 - 39 (The commented out functions) is where I'm sure is causing this error because once I don't comment them out pretty much everywhere Flink or Rlink is used or defined I get this error.
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?
Write a recursive function called sumover that has one argument n which is an unsigned integer. the function returns double value which is the sum of reciprocals of the first n positive integers =.
for example sumover 1 returns 1.0 sumover 2 returns 1.5 like 1/1+1/2
I'm currently making a game and what happens is that during runtime, it suddenly closes and a message is shown in the console saying "Pure virtual function called at runtime".
Here is the code: [URL]
The problem seems to occur somewhere between lines 662 - 695. And it seems to only happen when the size of the vector reaches 1.
So im trying to create a program that will calculate a shipping cost. I'm stuck on trying to calculate the actual milage and cost. Example being 1400 miles for a 2lb package would be $2 for every 500miles. so it would cost $6. But Im having trouble getting the program to round up the 1400 to 1500 so it charges the correct amount.
I know there would have to be a way to do this without programming all the conditions.
Also the course has not reached loops yet.
Code:
// This program will calculate the shipping charges.// The shipping rates are based on per 500 miles shipped. // They are not pro-rated, i.e., 600 miles is the same rate as 900 miles or 1000 miles. #include <stdio.h> #include <stdlib.h>
I want to have a program that will calculate the sum of the fourth powers of 100 real numbers in an array (numbers in this array are stated explicitly in the code), code I have for integers is working perfectly but i want it to work also for non integers.
Code:
#include "stdafx.h" #include "math.h" int sum_array(int a[], int numelements) { long double i, sum = 0; for (i = 0; i<numelements; i++) { sum = sum + pow((float)a[i], (int)4);
[code]....
The error messages i have: return' : conversion from 'long double' to 'int', possible loss of data.
I have a function and i want to delete a file when the function is called and starts it's loop i have used this code but unfortunately the file is not deleted ?
Code: void evaluate(void) /*evaluate the population */{ int mem; int i; double x[NVARS+1]; char buffer[101] = {"save.txt"};
I am using codeblocks for school and I am trying to create a program to calculate a percentage for commission and a bonus. I have tried using a decimal for percentage, int and floats with casting. Every time it I try it displays 0 or just the bonus. I even took and did a test page. My head is about to pop.
Write an algorithm that can be used to calculate the commission earned in a real estate transaction. The chart below describes the formulas used to calculate the commission.
Sales Price Commission Less than $100,000 5% of Sales Price $100,000 to $300,000 $5,000 + 10% of Sales Price over $100,000 More than $300,000 $25,000 + 15% of Sales Price over $300,000
// Commission and Bonuses. #include <iostream> #include <iomanip>
My program seems to be working fine, except for when I call on my delete function. I don't receive any errors, but when I call the delete function my program outputs nothing and freezes. As in, my print function (which is called before the delete function) doesn't even work. I've tried removing bits of the function to see if I could pinpoint where exactly the issue is, but I've had no luck.
#include <iostream> #include <string> using namespace std; class List{ private: struct node{ string _data;
[Code] ....
And the function that is causing me trouble:
void deleteNode(string data){ node* del = NULL; t = h; n = h; while(n != NULL && n->_data != data){
I need to create a program that reads some numbers, and calculate them on a separated subroutine, and the return of this subroutine must be the sum of all the numbers. I'm getting an error but I can't figure out why =/
#include <iostream> using namespace std; int calc(int val, int qtd){ int sum=0; for (int cont=0; cont<qtd; cont++) sum=sum+val; return sum;
[Code]...
The error that I'm getting is on the line 22.
The error message: "invalid conversion from 'int*' to 'int' [fpermissive]
I've created a function where you can choose any bounds for an array based list (positive or negative, as long as the first position is smaller than the last position). However for some reason when I call the print() function in my application program it doesn't do anything. My print function is technically correct (I still have work to do on the output) but I can't figure out why it wont show anything at all. Below is my header, implementation, and main program files, along with results from running the program.
I have a struct called Array and I'm to create a function to create a dynamic array that's fill with randomly generated integers from 0 to 50 (inclusive) and a function to destroy the array for freeing its memory. Below the code that I have written so far.
Code:
* Struct */ typedef struct {int *pArray; //the dynamic array int length; //the size of the dynamic array}Array; /* Function to create a dynamic array */ Array *initializeArray (int length) {int i; }
I'm trying to create a program to calculate the flight time and fuel burn of an airplane. However when I repeated the whole calculation, it did not show any data or result like the previous calculation. In addition, the "If" statement that I used to test the "fuelRemain" doesn't work properly. I can't figure out why.
Code: ig_cfunc.cpp:1609: error: `calc_rel_branch_degree' undeclared (first use this function)
I have deleted all of the objects and recompiled to make sure that everything is in sync. I can't see what the issue is here and I'm not sure what to do next. This code actually worked until I changed the name of the function, so the types are all correct and such. This is the format I use for all external functions ....
#include <iostream> class Hello { public: void Test() {
[Code].....
As i know a non-constant member function cant be called inside a constant member function but how the above code has been compiled successfully and giving the expected result .