Solved

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

Posted on 2003-10-31
4
230 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

762 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now