Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8950321
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:25:41+00:00 2026-06-15T13:25:41+00:00

float a, b; float sa() { return a;}; int main() { a = 10;

  • 0
float a, b;
float sa() { return a;};
int main() {
  a = 10;
  b = sa();
  printf("%f", b);
  return 0;
}

This is a simplified version of my code.
I believe the program should print 10 but it gives me really small numbers like -65550, not always the same but very alike.

I have used the debugger to check the value of variabe a right before it is returned and it is 10, so the function returns 10, but b is set to something like -65550. I don’t understand why this happens.

I’d appreciate some intell.

Thanks in advance.

Here is the full code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>

int dimensiuni, nrBitiSolutie, bitiPeDimensiune, gasitInbunatatire, nrRulari;
float limInf, limSup, precizie, valoareFunctie, minim, minimNou, T;
char solutie[100000];
float solutieReala[100];

void generateRandomSolution();
void bitesToFloat();
void rastrigin();
void rosenbrock();
float nextFirstFit();
float nextBestFit();


void main() {
    int k;
    T = 10;
    gasitInbunatatire = 1;
    srand ( time(NULL) );
    printf("Introduceti numarul de dimensiuni: ");
    scanf("%d", &dimensiuni);
    printf("Introduceti limita inferioara si cea superioara: ");
    scanf("%f%f", &limInf, &limSup);
    printf("Introduceti precizia: ");
    scanf("%f", &precizie);

    //calculam numarul de biti necesari ca sa reprezentam solutia
    nrBitiSolutie = dimensiuni * ceil(log(limSup-limInf * pow(10, precizie)))/log(2.0);
    bitiPeDimensiune = nrBitiSolutie/dimensiuni;

    //generam o solutie random
    generateRandomSolution();
    bitesToFloat();
    rastrigin();
    minim = valoareFunctie;

    printf("Pornim de la %f\n", minim);

    while( (nrRulari < 10000) && (T > 0.001)) {
        minimNou = sa(); //error occurs here. sa() returns about 200 but minimNou is set to -65550
        if (minimNou < minim) {
            printf("Minim nou: %f\n", minimNou);
            minim = minimNou;
            T *= 0.995;
        }
        nrRulari++;
    }
    printf("Minimul aproximat: %f\n", minim);
    system("pause");
}

void generateRandomSolution() {
    int l;
    for (l = 0; l < nrBitiSolutie; l++) solutie[l] = rand()%2;
}

void bitesToFloat() {
    int i, parcurse = 1, gasite = 0;
    int variabila = 0;
    float nr;
    for (i = 0; i < nrBitiSolutie; i++) {
        variabila = variabila<<1 | (int)solutie[i];
        if(parcurse == bitiPeDimensiune) {
            nr = (float)variabila / (float)pow(2, bitiPeDimensiune);
            nr *= limSup-limInf;
            nr += limInf;
            nr *= pow(10, precizie);
            nr = (int)nr;
            nr /= pow(10, precizie);
            parcurse = 0;
            solutieReala[gasite++] = nr;
            variabila = 0;
        }
        parcurse++;
    }
}

void rastrigin() {
    int i;
    valoareFunctie = 10 * dimensiuni;
    for (i = 0; i < dimensiuni; i++) {
        valoareFunctie += pow((float)solutieReala[i], 2) - 10 * (float)cos(2 * 3.14 * (float)solutieReala[i]);
    }
}

void rosenbrock() {
    int i;
    valoareFunctie = 0;
    for (i = 0; i < dimensiuni - 1; i++) {
        valoareFunctie += 100 * pow((solutieReala[i+1] - pow(solutieReala[i], 2)), 2) + pow((1-solutieReala[i]), 2);
    }
}

float sa() {
    int j;
    for (j = 0; j < nrBitiSolutie; j++) {
        solutie[j] = solutie[j] == 0 ? 1 : 0;
        bitesToFloat();
        rastrigin();
        if (valoareFunctie < minim) return valoareFunctie;
        else if ( (rand()/INT_MAX) < exp((minim - valoareFunctie)/T) )  
            return valoareFunctie;
        else solutie[j] = solutie[j] == 0 ? 1 : 0;
    }
    return minim;
}

I have marked where the error occurs with error occurs here comment

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T13:25:42+00:00Added an answer on June 15, 2026 at 1:25 pm

    You simplified the code incorrectly. In your simplification, you defined sa() before calling it. But in your full program, you call sa() before defining it. In the absence of a declaration, functions are assumed to return int. Since your function actually returns a float, the result is undefined. (In this case, you will read a garbage value from the top of the floating point stack and then the floating point stack will underflow, and things go downhill from there.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this simple code: int main() { float x = foo(); printf(returned value:
#include <stdio.h> int main() { float a = 1234.5f; printf(%d\n, a); return 0; }
int main(void) { int x; float y; x=10; y=4.0; printf(%d\n,x/y); return 0; } I
#include<stdio.h> int main() { float a; printf(Enter a number:); scanf(%f,&a); printf(%d,a); return 0; }
#include <stdio.h> #include <math.h> int main (void) { float inches; printf(Enter the number of
I have this simple program #include <stdio.h> int main(void) { unsigned int a =
I made this program to print up time in hours,minutes and seconds #include<stdio.h> int
What is wrong in this code even the printf is not working. but it's
There must be something I'm misunderstanding, why does this not return 10? int main()
#include<stdio.h> int main() { int x; float peri ,area1; scanf(%d,&x); area(x, &peri, &area1); printf(

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.