C/C++ :: Necessity Of Global Variable For Recursive Function

Nov 30, 2014

If I define the variable 'total', everything works well without any problem. But if I define it inside the function 'sum', I get irrelevant results, because each time the function gets executed, the variable total gets defined again, losing its value already assigned to it.

Do I have to use a global variable? Is there any way that I can do with without using a global variable?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int total;
int sum(int a, int B)/>{
if (a < B)/>{
//printf("The total is %d, a is %d, and b is %d
", total, a ,B)/>;
total += a;

[Code] ....

View 4 Replies


ADVERTISEMENT

C++ :: Global Variable - Calling Sub Function

Dec 19, 2013

Expected output: 20

But what I got is: 22

Why. While calling sub function it should take the global variable am I right

insert Code:
#include <iostream>
using namespace std;
int a=0;
void sub()

[Code] ....

View 3 Replies View Related

C :: Transform Local Variable Into Global Variable?

Oct 25, 2014

I need to transform a local variable into a global variable so I can use it in one of my functions. I thought about passing the value as a parameter to this function but I can do this since the function is called inside the while loop and this variable counts how many times the while loop does (so the final value is outside the loop). Example to visualize better:

Code:
while(condition) {
function(parameter1, parameter2);
count = count + 1;
}
printf("%d
", count);

So, I need to transform the final value of "count" into a global variable. Can I do this?

View 5 Replies View Related

C :: Global Variable - Only Printf Works

Apr 20, 2013

Okay so I am programming an 8051 variant to interact with an SD card. In a separate initialization routine in SD_CARD.c I pull out the vital information of the card into global variables. I then call Menu() which is in another source file but includes a header file with all of the variables declared as extern. Now here is the weird, part this is from my Menu.c

printf("%u" , VOLUME_1_SECTOR);
if(VOLUME_1_SECTOR==16384)
printf("Correct");
else
printf("Incorrect");

Now the output of the first printf is 16384 but the conditional evaluates to false. If I put this code in SD_CARD.c (Where VOLUME_1_SECTOR is defined) the conditional evaluates to true. I am confused why the printf works correctly in Menu.c but not the conditional.

View 2 Replies View Related

C :: How To Scanf Global Variable Outside The Main

Sep 7, 2013

The problem that I want to make an array " vertextDegree [nbColours] " with " nbColours " elements in it ,but the "nbColours" unknown and I have to get it get it from a file .

Code:

int nbEdges,nbVetices, nbColours ;
typedef struct st_graphVertex {
int index;
int colour;
int val ;
int vertexDegree[nbColours]; // it won't work because nbColours unknown
// here and I want get it from file in the main
struct st_graphVertex *next;
t_edgeList *out;
}t_grapheVertex;

View 3 Replies View Related

C++ :: Private Global Variable To Class?

Sep 30, 2014

I want to make a destructor counter...So it needs to be outside that specific instance of the class. And i want to keep it hidden. I don't want to be able to access it from outside the class...I Don't want main.cpp to have access to this variable

Is there a way to do this?

static int destructorCount;

View 8 Replies View Related

C++ :: Changing Global Variable In A Thread

Jul 28, 2013

I have a small problem with my program. It is kinda a mess but I will try to explain you what I am trying to do. I have some threads. One of it, it attempts to detect a game client. So my code is sort of like that:

DWORD ProcessID; // The process ID of the game client
void test() {
char* text;

[Code]....

So basically, its like the variable changes, but only inside the thread... why does that happen?

View 15 Replies View Related

C/C++ :: How To Declare Global Variable In Structure

Apr 28, 2015

I am getting this error while trying to compile my program:

It says that my variables "nome, cognome, eta..etc" are being used for the first time in my "inserisci" function.

I tought that I could just declare them as global in my structure like I did in my code, but apparently this doesn't work.

Do I really need to declare them again outside of my structure? Isn't there another way?

Here is my code:

#include <stdio.h>
#include "readline.h"
void inserisci(void);
struct impiegato{
char nome[20];
char cognome[20];
int eta;

[code].....

View 7 Replies View Related

C/C++ :: Global Variable Extern Typedef Int?

Oct 8, 2014

I have a .c file for each function of my program and i have .h,so how can I extern the following

in my main...

typedef int table[100][100];

how can I extern them in the .h and c files?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#define HEIGHT 100
#define WIDTH 100

[code]....

View 3 Replies View Related

C++ :: Creating Global Variable / Pointers?

Jan 6, 2012

I am making two classes using the juce library [URL] ....

this is a problem that has now come up after solving an earlier problem i posted but its a completely seperate issue.

i have a class for loading a file when i button is clicked that seems to work. Here is the function in my LoadButton cpp:

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File pedoFile (pedoFileChooser.getResult());
}
}

I want to be able to then use the file stored in pedoFile in the cpp of another class called PlayButton. I tried doing this with a pointer? not sure if that's correct way of doing it (i know very little about C++ or programming) by changing the function to this. I'm getting the error invalid initialisation of non-const reference of type 'juce::File*&' from a temporary of type 'juce::File'

void LoadButton::buttonClicked(Button* button)
{
FileChooser pedoFileChooser ("Choose an Audio File", File::nonexistent, String::empty, true);
if (pedoFileChooser.browseForFileToOpen())
{
File* &pedoFile = (pedoFileChooser.getResult());
}
}

View 3 Replies View Related

Visual C++ :: Global Variable Scope

Jun 15, 2014

//I dont understand this why does "<< "
The value of global now is: " << global << "
";" is equals to nine

#include <iostream>
int subtract (int a, int b);
int global = 5;
int main(void) {
using std::cout;
int a, b;

[Code] ....

View 1 Replies View Related

C++ :: Access Variable From Other Scope Without Global Declaration?

Aug 8, 2013

Code:
#include <iostream>
using namespace std;
void f() {
int x=17;
//cout<<main::y<<endl; i want to access y from main scope
}

int main() {
int y=23;
//cout<<f::x<<endl;

I want to access x from f scope is there any way for this without global declaration? specially about function scopes...

View 1 Replies View Related

C :: User Input Variable To Global Array

Oct 11, 2013

I am trying to create a global array with user-defined dimensions.the code is:

Code:

int matr_size()
{
int x = 0;
printf("Please enter the number of nodes: ");
scanf( "%d", &x);
printf("There are %d nodes in this simulation.", x);
getchar();
return x;
}

[code]....

I read that an array cannot be defined by a variable in C so I assume that is the issue, but I'm not sure how else to do it. Previously the size was defined by #define NODES and it worked fine but I need this user input.

View 5 Replies View Related

C/C++ :: Use Global Variable Stored In File In 2 Different Functions?

Mar 13, 2014

Ok so when the program runs the first function the data is stored and displayed in the file. The second function is supposed to read the name entered, compare it to the ones in the file then take the price with it BUT I seem to have done something wrong when reading the files (or maybe it has to do with the global function I'm not sure). Here's parts of the code :

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

[Code]....

View 7 Replies View Related

C/C++ :: Create Local Instances Of A Global Variable?

Oct 4, 2012

is it possible to have a global variable pointing to a different address depending on the thread?

Imagine one would like to use threads with the loop:

for (i=0;i<n;i++){
globalPointerVariable=getAddress(i);
DoThingsUsingThe_globalPointerVariable();
}

View 4 Replies View Related

C++ :: Defining Global Variable - Linker Error

Jun 18, 2012

If I DEFINE a global variable in two source files which belong to the same project, then there is a linker error "multiply defined symbols". But if I DEFINE a global variable in two source files which belong to the different projects, then it compiles fine. Why?

View 8 Replies View Related

C++ :: Any Variable Type That Can Change Its Value In Global Scope

Nov 17, 2013

is there any variable type(or with another keyword) that we can change it's value in global scope?

View 5 Replies View Related

C++ :: Difference Between Static Local Variable And Static Global Variable?

Aug 5, 2013

Here is the code,

Code:
class A {
};
A& CreateObject() {
static A a;
return a;
} static A aa;
int main() {
return 0;
}

So is there any difference between a defined in CreateObject and aa?

View 6 Replies View Related

C :: Save Data From Global Variable Into Linked List

Jul 6, 2013

I'm trying to run so called student-administration program.I got functions that can read and write student data, but the one saving the data from about 30 students has some problem that I can't figure. (warning: I'm quite new to C programming)so this is the code:..I guess I can't use global variables as function arguments?

Code:

//global variables
static char ReturnName[31];
static char ReturnFirstName[31];
static float ReturnAverage;
}

[code]....

incompatible types when assigning to type 'char[31]' from type 'int'

View 2 Replies View Related

C :: String Declaration As Global / Main Local Variable

Nov 14, 2013

When a declare a string e.g.

Code:
char str[30]; as a global variable, the srting is initialized to NULL.

But if I declare char str1[30] INSIDE main(), then the string has garbage inside.... Why this happens??

E.g. the following code snippet...

Code:
#include <stdio.h>
char str[50];
int main(){
char str1[50];

[Code] ....

View 4 Replies View Related

Visual C++ :: Cannot Use Global Scope Max Function

Jan 30, 2013

The compiler "forces" me to use the std::max method, I cannot override it even when writing ::max in my code.

View 4 Replies View Related

C++ :: Global Arrays - Passed By Reference To Function

Mar 9, 2013

I currently have multiple functions that use globally declared arrays in my code.

I want to turn them so that arrays are no longer globally declared, but instead are passed by references to the function.

And I have a function caller inside main, and other functions are called within the function.

View 3 Replies View Related

C++ :: Translating Recursive Function Into Iterator Function

Nov 27, 2014

How do I change the following recursive function into a iterator function and basically what should it look like?

int f(int i) {
if(i<2)
return i;
return f(i-2)+f(i-1);
}

View 1 Replies View Related

C++ :: Changing Recursive Function Into Iterator Function?

Nov 29, 2014

I had the following question in my exam and received 3 out of a possible 4 marks

Here is the question:

Assume the following recursive function:
int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

Translate this function into an iterative version:

Here is my solution:

int f(int i)
{
int prev,next,final;
prev = 0;

[Code]....

View 9 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i)
{
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i)
{
int prev,next,final;
prev = 0;
next = 1;
if(i==1)
final = i;

[Code] .....

View 1 Replies View Related

C/C++ :: Changing Recursive Function Into Iterator Function?

Nov 30, 2014

I am trying to translate the following recursive function into its iterative function:

int f(int i) {
if(i < 2)
return i;
return f(i - 2) + f(i -1);
}

This is what I tried to do:

int f(int i) {
int prev,next,final;
prev = 0;
next = 1;

[Code] ....

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved