Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Problem with simply reading in values, no idea what could be wrong.

Posted on 2003-10-31
4
232 Views
Last Modified: 2010-04-15
Alright, I'm completely beat as to how my program cannot be working. I'm having trouble simply reading in some numbers to use.

After the third scanf, when running the program, the program sort of freezes. The cursor blinks, but nothign can be entered, i can only kill the window.

The program is not complete of course, but it should at least be able to read in the 3 numbers, "reli", "trials", and "seed". I should also be able to get to the system pause.

Here's the code, help me if you can, thanks.

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

int main()
{

int trials, flag, i, c, j, k;
float reli, seri, para, comp, counts, countp, countc;
unsigned int seed;

printf ("Please enter the decimal value of you component reliablility: ");
scanf ("%f", &reli);

printf ("Please enter the number of trials you'd like to perform: ");
scanf ("%f", &trials);

printf ("Please enter the seed value for the random number: ");
scanf ("%u", &seed);

srand(seed);

for (i=0; i<=trials; i++)
{
      flag=1;
      for (c=0; c<3; c++)
      {
          seri = ((float) rand()/RAND_MAX);
          if (seri>reli)
             flag=-1;
      }
      if (flag==1)
         counts++;
}


      system("PAUSE");
      return 0;
}
0
Comment
Question by:aonu
  • 2
  • 2
4 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 125 total points
ID: 9660105
> scanf ("%f", &trials);
The 'trials' variable is an int. Should be 'scanf("%d", &trials)'.
0
 

Author Comment

by:aonu
ID: 9660406
What... a... stupid... mistake...

*shoots self*

thanks.

PS: Does my srand look ok? The idea is to have a seed set for the random number generator, and I've only gotton vague answers.
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9660489
The srand() looks fine. Note that the result of the rand() function depends only on the supplied seed and is therefore not random at all. The sequence generated by rand() does have some nice statistical properties though. You can also seed the random number generator using the current time (srand(time(0))) if you simply want the output of the program to be different each time it is run.
0
 

Author Comment

by:aonu
ID: 9660570
Aha, I'll have to keep that in mind, although the program requires some control over the seed, somewhat ironically.

Thanks very much for your help, I hope the rest of my program goes smoothly enough
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question