C/C++ :: Multiple For Loops - Independent Function Change
Feb 28, 2015
I'm new to writing C. While doing for loops I have found that when I try to do functions and have multiple for loops, even though I have different counters, the independent functions change. For example:
int factorial(n) {
for(i=1;i<n;i++)
fact=fact*i;
return fact;
[Code] ....
The cosine function works out fine but the factorial one spits out crazy numbers, more specifically, 6^1 through 6^10. In my head, the factorial should be a constant throughout the second loop for I guess I am wrong. Why this happens?
I've been given an assignment called a Single Facility Layout Problem in which a number of machines (for the purpose of the assignment they're called machines) with an x-coordinate, y-coordinate and weighting are given/inputted. I'm using 5 machines for ease of use and reading errors.
The object of the program is to calculate the value of each set of coordinates using the Me formula, the lower the value, the better. Thus the program should output a 5 different values.
Problem: My problem is that instead of getting, say 5 unique values, I'm getting 5 of the same value much like the photo attached.
- How can I remedy this? - Is there a limit to the amount of FOR loops that can be nested? If not why isn't my code working correctly? - Where should I place the printf statement in order that I will output 5 unique values?
#include <stdio.h> #include <stdlib.h> #include <math.h> int main() { //'h' is number of machines, objects etc. int h=5;
I am trying to find a way to put a getSmallest function in here so that it will output smallest integer entered. If it is just an arbitrary amount of #'s and I don't know what will be entered I am confused. Both on how to do it and how to link my function to my loop.
Code: /* Prompts user and gets integer numbers from keyboard, one number at a time. Ends program when 99999 entered. and displays various results. Written by: Date: 10/20/14 */ #include <stdio.h> #include <stdlib.h>
[Code] .....
Could I take this and just replace all variables a, b, and c with getNumber, where would I link/declare smallest?
Code: /* ==================== smallest ====================== This function returns the smallest of 3 integers. Pre given 3 integers Post the smallest integer returned */ int smallest (int a, int b, int c) {
I'm currently writing a poker game and am trying my best to avoid using global variables. I have a few variables in int main() which i was hoping to use to store the value of each players hand. I then created a function which calculates the value of the hand but cannot get this value back into the main function.
For example:
Code: #include <iostream> using namespace std; void getValue(int value) { value = 4;
[Code] ....
Is there any way i can get the value of value using this function? If not what can I do?
#include <stdio.h> void define (int integer, int IntArr[0], int *IntP); int main(void) {int integer = 0, IntArr[1] = {0}, IntP = 0; define(integer, IntArr, &IntP);
[Code]...
Why does the integer with array change after passing trough the function and the normal integer doesn't? (I know why the normal one doesn't, but I dont get the array one)
when i pass a string pointer to a function such as string *str = new string(""); and pass that string to a handleElement() function e.g. handleElement(str), and i change the value in the function it simply doesn't change when the function exits as though it's passing it by value or something, even though it gives the pointer address.. I've now changed the code to use double pointers and pass the reference of the str pointer and it works but it seems ugly to use double pointers for this.
//handles when a new element is encountered when parsing and adds it to the parse tree bool ParseBlock::handleElement(char cur, string *curString, int count, bool isOperator) { countNode = new ParseNode(count); //keep track of numbers and strings if they exist and insert them if(!curString->empty()){ if(isNumber(*curString)
I need to create a generic function that changes from any starting base, to any final base. I have everything down, except my original function took (and takes) an int value for the number that it converts to another base. I decided to just overload the function. I am Ok with changing between every base, but am slightly off when using my new function to take in a string hex value.
The code below should output 1235 for both functions. It does for the first one, but for the second, I am currently getting 1347. Decimal to Hex works fine - It's just the overloaded function (Hex to anything else) that is slightly off.
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num); void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);
We are programming a function calculator and my instructor hinted that the add() function is almost exactly like the subtract() function and we would only have to write one function if we "called it in a special way." This got me thinking, can I turn a function's argument to negative during the call to make it subtract?
Example:
void addSub(int &num1, int &num2) into this void addSub(int &num1, int -(&num2))
My coin/money change code works when there can be an exact change each time, i.e. when the 1 cent option is available. However, when the change options are only $10, $5, $1, 25 cents and 10 cents, it does not give me what I want for instance, I wanted to get change for $237.80, I was expecting to get:
23 10's, one 5, two 1's and 8 dimes. However, the code below is giving me 23 10's, one 5, two 1's and 3 quarters (there is no option left for the 5 remaining cents).how to fix it?
Code: #include<iostream> #include<algorithm> #include<vector> using namespace std; void change(double cents, int a[]); int main() { double Dollars; double cents;
I am an IT student currently learning linked list. I have a problem with my code here. After I call addFront() my list doesn't change when I display it. How do I somewhat change/fill my list without changing the function type? I know it works on pointers still messed up with linked list.
This code ran well until i added in the ToLower function which is supposed to convert the char array string to lower case (based off ascii strategy -32). correct this function so it converts string to lower case and doesn't get errors.
#include <iostream> #include <string> using namespace std; const int MAX = 81; //max char is sting is 80 void ToLower(char s[]); int main(){ string y_n;
I am actually developing an nginx module in C.I am not to bad in C, but i got a big problem to pass argument to a vadiadic function.This function look like the well good old printf, but you put a buffer as first argument, the last address to stop to put data as second argument (in my case it is the last adress of disponible memory), a string that look like one in printf, an the other argument after.Here is the problem, the 4th last argument does not have the good value. In fact, It seem to be random value from memory. I Use gcc (Debian 4.9.1-19) 4.9.1.
I am using a library X that has functions x,y,z plus some others. also i am using a library Y that has those same functions (x,y,z) plus some others. (so both libraries have certain objects that are shared). libraries are designed to do different things and i need them both . However when i load them both i get
sem.c.text+0x2c10): multiple definition of `upper' ...
errors.
libraries are big and rewriting is not an option for me. Question: how do i bypass this problem?
I am reading a file of text. I want to read in every word, but no spaces or newlines. "word" is a string, and "c" is a char (used for getting rid of white space. The problem: I can get rid off spaces perfectly, but newlines remain in "word" if it comes before the terminating character ' '.
In the below code I'm having trouble calculating the algebraic equation on the line marked with &&&. I attempt to calculate it both within the member function Energy(x) and within find_kin_en(x), but in the latter I find the result equal to zero, which is wrong and disagrees with the correct value calculated in Energy(x). I think the problem might be having multiple nested member functions, i.e. operator() calls Energy(x) which calls find_kin_en().
#include "/u7/tolsma/Numerical_Recipes/nr_c304/code/nr3.h" // these are numerical recipes libraries, not important for the problem below I believe. #include "/u7/tolsma/Numerical_Recipes/nr_c304/code/mins.h" #include "/u7/tolsma/Numerical_Recipes/nr_c304/code/mins_ndim.h"
Get user input should be done in a function. The function should read the product number, price per unit, and quantity sold and return them to the main().
Display the value of product number, price per unit and quantyty sold in main().
I have a function, int teleport_to_game(int,float,float); in my class. My question is should I change the int to define a function to a different type?
I basically need to take line1, line2, and line3 and return them to the reverseString function. Also, I am not allowed to do anything like make my own classes. I have to stick to the basics and no higher level programming techniques since we have not learned them yet.
Write a program using inheritance allow user to enter grades of his students 5~8 students as a base class and compute the sums for each students in derived class and compute the average of sums in another derived class. I created 3 classes in 1 header file and 1 cpp file how ever i cant seem to get the sum or the average to show up on execution time