I have an assignment that asks for me to write a program that can generate a ran
ID: 3621261 • Letter: I
Question
I have an assignment that asks for me to write a program that can generate a random number, then show how many characters are in together. then it will generate another random number from 1-1000 and show how many characters, then add the number of characters from the first number onto the second number.for example
number characters total characters
1 1 1
10 2 3
100 3 6
1000 4 10
45 2 12
e.t.c. for up to ten times
I did in in C language. Can someone help me? i keep getting a few error messages when compiling it. Here are the error codes:
in function 'getRandNum':
syntax error before ';' token
warning: no newline at end of file
I dont know what all of this means. Can tell me what's wrong and tell me how to correct it? Please also make hte correction in my code. thanks!
this is my code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* outputs a random number */
int getRandNum(int a, int b) /* generates random number */
{
return (a + rand()%(b-a+1);
}
int getCharsInNum(int n) /*function for showing number of character*/ {
if (n < 10)
return 1;
else if (n < 100)
return 2;
else if (n < 1000)
return 3;
else
return 4;
}
int main() {
srand(time(NULL)) /*gets random number*/;
int a[10]; /*array*/
int numChars; /* number of characters*/
int totalChars = 0; /*total number of characters */
int i; /*control loop variable*/
for (i = 0; i < 10; i++) {
a[i] = getRandNum(1, 1000);
numChars = getCharsInNum(a[i]);
totalChars = totalChars + numChars;
printf("%d %d %d ", a[i], numChars, totalChars);
}
return 0;
}
Explanation / Answer
please rate - thanks
you were missing a ) and one was in the wrong place.
you need an enter after the last }
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* outputs a random number */
int getRandNum(int a, int b) /* generates random number */
{
return a + (rand()%(b-a+1));
}
int getCharsInNum(int n) /*function for showing number of character*/ {
if (n < 10)
return 1;
else if (n < 100)
return 2;
else if (n < 1000)
return 3;
else
return 4;
}
int main() {
srand(time(NULL)) /*gets random number*/;
int a[10]; /*array*/
int numChars; /* number of characters*/
int totalChars = 0; /*total number of characters */
int i; /*control loop variable*/
for (i = 0; i < 10; i++) {
a[i] = getRandNum(1, 1000);
numChars = getCharsInNum(a[i]);
totalChars = totalChars + numChars;
printf("%d %d %d ", a[i], numChars, totalChars);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.