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?
View 6 Replies
ADVERTISEMENT
Mar 15, 2015
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;
[Code] .....
View 3 Replies
View Related
May 30, 2013
Is it possible that have a function or class can generate independent random number . And I can use it any time.
View 3 Replies
View Related
Aug 17, 2013
I have tried to pass an array to a function. Why my code doesn't work for a[1][0], a[1][1] etc.
Code:
#include <stdio.h>
int i=0;
int j=0;
int main(void){
int a[3][3]={1,2,3,1,2,3,1,2,3};
void f(int a[3][3]);
[Code] ....
I think my problem is with the for loops. I will try to find the error a bit more.
View 6 Replies
View Related
Oct 22, 2014
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) {
[Code] ......
View 1 Replies
View Related
Jan 30, 2014
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?
View 8 Replies
View Related
Jul 4, 2013
Which value change after call reverse function.
#include<iostream>
using namespace std;
static void reverse (int len, char *buf) {
char tmp[24];
int i;
[Code] ....
View 10 Replies
View Related
Apr 12, 2014
Code:
#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)
View 1 Replies
View Related
Oct 15, 2014
I'm trying to write a program to test if a string is palindromic using only pointers
Code:
#include <stdio.h>
#include <string.h>
void revstr(char* str)
}
[code]....
I need to change the arrays in the function int palin to pointers.
View 4 Replies
View Related
Dec 24, 2014
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)
[code].....
View 2 Replies
View Related
Oct 6, 2014
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.
#include <iostream>
#include <stack>
#include <string>
#include <cmath>
using namespace std;
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, int num);
void switchBasesFunction(stack<int> & myStack, int startBase, int finalBase, string s);
[Code] .....
View 2 Replies
View Related
Feb 28, 2015
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))
Would this work when passing by reference?
View 9 Replies
View Related
Feb 20, 2013
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;
[code]...
View 14 Replies
View Related
Feb 13, 2013
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.
Code:
typedef struct node *nodeptr;
struct node{int item;
nodeptr next;};
typedef nodeptr List;
}
[code]....
View 3 Replies
View Related
Apr 8, 2013
This code gets a binary number and change its bits. I have a problem with the "bits_up" function . Why this function not working?
Code:
#include <stdio.h>
#include <stdlib.h>
int bits_up(uint first,uint last,int *ptr);
int main(void)
{
uint first,last,bitUD;
int InputBinNumber[4],updatedNum[4];
[Code] ....
View 5 Replies
View Related
Feb 17, 2014
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;
[Code]...
View 1 Replies
View Related
Jan 15, 2015
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.
Code:
/* ngx_html_log.h */
#ifndef NGX_HTML_LOG_H
#define NGX_HTML_LOG_H
#include <ngx_vasm.h>
}
[code]...
View 3 Replies
View Related
Feb 27, 2014
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?
View 1 Replies
View Related
Oct 10, 2013
How would I write a function that determines if a number is a multiple of another number.ex. (is 147 a multiple of 7?)
View 5 Replies
View Related
Sep 11, 2014
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 ' '.
My code:
while(infile.good() && !infile.eof()) {
while(infile.peek() == ' ')
infile >> c;
while(infile.peek() == '
[Code] .....
View 3 Replies
View Related
May 15, 2014
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"
[Code]....
View 1 Replies
View Related
Feb 25, 2013
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().
View 7 Replies
View Related
Dec 10, 2014
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?
View 2 Replies
View Related
Mar 4, 2015
I need to pass multiple values from one function to another and how to do this. Here is my code so far.
#include <iostream>
#include <string>
#include <cctype>
#include <fstream>
using namespace std;
//Function Prototypes//
int charString(string, string, string);
string reverseString(string, string, string);
[Code]...
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.
View 5 Replies
View Related
Oct 13, 2013
in a function how do you return multiple values to the main function.
View 4 Replies
View Related
Dec 17, 2014
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
header file
#ifndef GRADES_H_INCLUDED
#define GRADES_H_INCLUDED
class Grades {
public:
Grades()
[Code] .....
View 2 Replies
View Related