I really have no clue why this won't compile. Some feedback would be appreciated. If possible, I would just like small hint or a point in the right direction instead of giving me the answer. Thanks much, folks.
The purpose of this code is as follows:
./a.out R S
R is the number of rolls
S is the seed for srand
if not supplied, R is 30 S is 13
can supple R&S, R, none
roll 2 dice; frequency of the sum of 2 values
use 11 sided dice
output should be well formatted & informative
#include <stdio.h>
#include <stdlib.h>
int main( int argc, int argv[] )
{
int dice1, dice2, sum;
int R, S, k, g, j, x, count = 0;
int list[22];
int nums[22] = {0};
for( j = 2; j < 23; j++ )
{
list[j] = j;
}
if( argc != 3 )
{
R = 30;
S = 13;
}
else
{
R = argv[1];
S = argv[2];
}
srand( S );
for( k = R; k > 0; k-- )
{
dice1 = rand() % 11 + 1;
dice2 = rand() % 11 + 1;
sum = (dice1 + dice2);
for( g = 2; g < 23; g )
{
if( sum == list[g] )
{
nums[count] += 1;
count++;
}
}
}
count = 0;
for( x = 2; x < 23; x++ )
{
printf("Frequency of %d: %d\n", x, nums[count]);
count++;
}
return 0;
}
You should be able to do it yourself. It's more than basic. Check the comments.
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] ) // argc is number of arguments, argv[][] is list of arguments that are strings
{
int dice1, dice2, sum;
int R, S, k, g, j, x, count = 0;
int list[23]; // increase size of these arrays as you will go out of bounds
int nums[23] = {0};
for( j = 2; j < 23; j++ ) // j==22 is out of bounds, if you need j==22, increase `list' and `nums' size (above)
{
list[j] = j;
}
if( argc != 3 )
{
R = 30;
S = 13;
}
else
{
R = atoi(argv[1]); // convert string to int
S = atoi(argv[2]);
}
srand( S );
for( k = R; k > 0; k-- )
{
dice1 = rand() % 11 + 1;
dice2 = rand() % 11 + 1;
sum = (dice1 + dice2);
for( g = 2; g < 22; g++ ) // do something with g, like increment for example, as it's infinite loop
{
if( sum == list[g] )
{
nums[count] += 1;
count++;
}
}
}
count = 0;
for( x = 2; x < 23; x++ )
{
printf("Frequency of %d: %d\n", x, nums[count]);
count++;
}
return 0;
}
Here is a diff that will hopefully help you to see the difference. You can get it yourself with $ diff -rup rst1.c rst2.c > rst2.diff.
PS. I think all things had been pointed in the comments so you should give thumb up to everyone of them.
Related
here's the sample of the input and the output
What's wrong with my code? The output on my Dev-C++ is alright, no problem, but when I submitted it to an online judge it turns out as wrong answer. I don't know what's the problem, and I prefer to not change most of them (because this code turns out from my difficult brain, I'm sorry). But any help would be appreciated, thank you.
#include <stdio.h>
int main () {
int T = 0, max = 0, X, sum = 0;
scanf ("%d", &T);
long long int N = 0;
//2891
for (int i = 0; i < T; i++) {
sum = 0;
scanf ("%lld", &N);
int A = N;
while (A / 10 != 0) {
A = A / 10;
max++;
}
int length = max;
for (int j = length; j >= 0; j--) {
int X = N % 10; //1
sum += X;
int Xx = N / 10; //289
int Y = Xx % 10; //9
int Yy = Y * 2; //18
if (Yy > 9) {
Yy -= 9; //9
}
sum += Yy;
N = Xx / 10;
}
if (sum % 10 == 0) {
printf ("PASS\n");
} else printf ("FAIL\n");
}
return 0;
}
this program is build to know all the huiwen number from 1 to 256
like 11^2=121 so 11 is a huiwen.
actually I have a good one
but I am confuse about why this program doesn't work well.
#include<stdio.h>
int is_huiwen(int l)
{
//DON'T WORK?WHY?
int number[20]={0};
int i,j,k;
int f=l*l;
for(j=0;f;j++)
{
number[j]=f%10;
f/=10;
}
for(k=0;j-k>0;k++,j--)
{
if(number[j]!=number[k] )
{
return 0;
}
}
return 1;
/* this is ok
int y=0;
int t = l*l;
int x = t;
do
{
y=y*10+t%10;
t /= 10;
}
while(t);
if(x==y)
return 1;
else
return 0;
*/
}
int main()
{
int i;
int flag;
for(i=0;i<256;i++)
{
flag=is_huiwen(i);
if(flag)
{
printf("%d is huiwen",i);
}
else
{
printf("\n");
}
}
printf("end\n\n\n\n");
return 0;
}
end.
The error is the if( number[j] != number[k] )line as pointed out by BluePixy.
The correction is if( number[j-1] != number[k] )
I would to point out that the code has hidden the problem partly because of a smattering of one letter variable names and inconsistent naming.
Here is the code that I used to find the same solution
int is_huiwen_2(int L )
{
int ret = 1;
int j = 0;
int k = 0;
int t = L * L;
int number[8] = { 0 };
for( j = 0; t; j++)
{
number[j] = t % 10;
t /= 10;
}
for( k = 0; (j - k) > 0 && ret; k++, j-- )
{
if( number[j-1] != number[k] )
{
ret = 0;
}
}
return( ret );
}
int is_huiwen_1( int L )
{
int ret = 1;
int y = 0;
int t = L * L;
int x = t;
do
{
y = y * 10 + t % 10;
t /= 10;
}
while( t );
if( x != y )
{
ret = 0;
}
return( ret );
}
int main()
{
int i = 0;
int flag = 0;
for( i = 0; i < 256; i++)
{
flag = is_huiwen_1(i);
if(flag)
{
printf("1: %d is huiwen\n",i);
}
}
for( i = 0; i < 256; i++)
{
flag = is_huiwen_2(i);
if(flag)
{
printf("2: %d is huiwen\n",i);
}
}
printf("end\n");
return( 0 );
}
I don't know what all the variables do. The use of 'l' looks like a '1' in some editors.
Being a Code Refactoring monk or zealot, I did some modifications such as reducing the code to one consistent return condition, value and point of return.
While it might seem overly simple, having multiple declarations on one line hides trouble, such as int i not being used in the second function.
The use of int numbers[20] = { 0 }; was also misleading us as for the range of values here [20] was overkill, having an explanation of what the range of values "12321", "1234321" where could have meant easier debugging of this code. A simple comment of what the outcome should look like would have solved several problems.
// A Huiwen number looks like this "12321", "1589851"
While this might be a simple exercise it portends greater problems down the line if this was not a trivial exercise. Try not to make it hard on the next coder.
Document your algorithms, arithmetic and variables please.
I wrote a simple program in c that accepts two numbers and then splits the first number considering the digits of the second number like this:
Input:
362903157 2313
Output:
36
290
3
157
Everything works just fine, except when there are zeroes in the first number, my program skips them. For instance the upper example gives me this output:
36 293 1 570
And that is mycode:
#include <stdio.h>
int nDigits(unsigned i) {
int n = 1;
while (i > 9) {
n++;
i /= 10;
}
return n;
}
// find the highest multiple of 10
int multipleOfTen(int num){
int multiple = 1;
while(multiple <= num){
multiple *= 10;
if(multiple > num){
multiple /= 10;
break;
}
}
return multiple;
}
int main(){
int n, m, digit;
scanf("%d %d", &n, &m);
int lengthOfM = nDigits(m);
for (int i = 0; i < lengthOfM; i++){
digit = m / multipleOfTen(m); //2
for(int j = 1; j <= digit; j++){
printf("%d", n/multipleOfTen(n));
n = n% multipleOfTen(n);
}
printf("\n");
m = m % multipleOfTen(m);
}
return 0;
}
What should I change in my program so that the zeroes won't be ignored?
Instead of calling multipleOfTen() in each loop, call it once and save the result for both n and m. Then in each loop divide those results by 10
#include <stdio.h>
int nDigits(unsigned i) {
int n = 1;
while (i > 9) {
n++;
i /= 10;
}
return n;
}
// find the highest multiple of 10
int multipleOfTen(int num){
int multiple = 1;
while(multiple <= num){
multiple *= 10;
if(multiple > num){
multiple /= 10;
break;
}
}
return multiple;
}
int main(){
int n, m, digit;
int i, j;
int n10, m10;
scanf("%d %d", &n, &m);
int lengthOfM = nDigits(m);
n10 = multipleOfTen(n); //get the multiple of ten once
m10 = multipleOfTen(m);
for ( i = 0; i < lengthOfM; i++){
digit = m / m10;
m10 /= 10;
for( j = 0; j < digit; j++){
printf("%d", n/n10);
n = n% n10;
n10 /= 10;// divide by 10
}
printf("\n");
m = m % multipleOfTen(m);
}
return 0;
}
I suppose an approach like this is inadmissible?
#include <stdio.h>
#include <string.h>
int main ( void ) {
char n[64];
char m[64];
char * p = n;
int i = 0;
int c;
scanf("%63[0-9] %63[0-9]", n, m);
while ((c = m[i++]) != '\0') {
int j = c - '0';
while (j-- > 0) if (*p) putchar(*p++);
putchar(' ');
}
putchar('\n');
return 0;
}
when n=903157 and after n = n% multipleOfTen(n); n becomes 3157 not 03157 so when u dividing again in line printf("%d", n/multipleOfTen(n)); it prints 3 not 0 what you want!!
Fix your code to produce right output.
The code is trying to find the largest palindrome made from the product of two 2-digit numbers. The answer is 91*99 = 9009 but I keep getting 990, which is not even a palindrome. I really appreciate the help!
#include <stdio.h>
int main()
{
int i = 10;
int j = 10;
int a = 0;
int b = 0;
int array[100] = {0};
int divider = 10;
int num;
int great;
int product;
int n;
int flag;
/*Loop through first 2 digit number and second 2 digit number*/
while (i<100)
{
while (j < 100)
{
product = i*j;
array [a] = product % 10;
n = product / divider;
while (n != 0)
{
a++;
num = n%10;
divider *=10;
array[a]=num;
n = product/divider;
}
flag = 0;
while (b<a)
{
if (array[b] != array[a])
{
flag = 1;
}
b++;
a--;
}
if (flag == 0)
{
great = product;
}
j++;
a = 0;
b = 0;
}
i++;
}
printf("The largest palindrome is %d \n", great);
return 0;
}
Here is a code snippet you can try.
#include <stdio.h>
void main()
{
int a = 1; // first integer
int b = 1; // second integer
int currentNumber;
int currentPalin; if a palindrome is found, its stored here
while (a<100){ //loop through the first number
while (b<100){ // loop through the second number
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){ //check for palindrome
currentPalin = currentNumber;
}
b = b+1; //increment the second number
}
b = a; // you could have set b=1 but it would not be an efficient algorithm because
//some of the multiplication would occur twice. eg- (54*60) and (60*54)
a = a +1; //increment the first number
}
printf ("Largest palindrom is %d \n", currentPalin);
getchar();
}
// method for finding out reverse
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
// when you divide a number by 10, the
//remainder gives you the last digit. so you are reconstructing the
//digit from the last
n = n/10;
}
return reverse;
}
Update:- As suggested by M Oehm, I have modified the code to make it more general.
#include <stdio.h>
void main()
{
int a = 1;
int b = 1;
int currentNumber;
int currentPalin=0;
while (a<100){
while (b<100){
currentNumber = a*b;
if (currentNumber == reverse(currentNumber) ){
if (currentNumber>currentPalin){
currentPalin = currentNumber;
}
}
b = b+1;
}
b = 1;
a = a +1;
}
if (currentPalin==0){
printf("No Palindrome exits in this range");
}
else {
printf ("Largest palindrome is %d \n", currentPalin);
}
getchar();
}
int reverse(int n){
int reverse = 0;
while (n != 0)
{
reverse = reverse * 10;
reverse = reverse + n%10;
n = n/10;
}
return reverse;
}
An alternative approach to solve the problem.
#include<stdio.h>
int reverse(int num)
{
int result = 0;
while( num > 0)
{
result = result * 10 + (num%10);
num/=10;
}
return result;
}
int main()
{
int last_best = 1;
int best_i=1;
int best_j = 1;
const int max_value = 99;
for( int i = max_value ; i > 0 ; --i)
{
for(int j = i ; j > 0 ; --j){
int a = i * j;
if( last_best > a )
break;
else if ( a == reverse(a) )
{
last_best = a;
best_i = i;
best_j = j;
}
}
}
printf("%d and %d = %d\n", best_i,best_j,last_best);
}
And it is quite simple to follow.
It seems that you do not reinitialize variables at the beginning of loop. They keeps values from previous iterations. For example, j and divider. Put
j = 10;
before starting "j" loop, i.e.:
j = 10;
while (j < 100) ...
The same for divider:
...
j = 10;
while (j < 100) {
divider = 10;
...
If you were using for loops you would avoid this problem naturally:
for(i=10; i<100; i++) {
for(j=10; j<100; j++) {
...
}
}
int i,j,vec[15]={0};
srand (time(NULL));
for (i=0;i<15;i++){
vec[i]=rand() % 25+1;
for (j=0;j<15;j++){
if (i!=j){
while(vec[i]==vec[j]){
vec[i]=rand() % 25+1;
}
}
}
printf("%d\n",vec[i]);
}
return 0;
}
the code still gives me repeated numbers
EXAMPLE:
24
3
7
20
18
10
12
17
9
7
4
25
13
15
21
I cant figure out what to do with it
You have your loops mixed up. The logic is: Generate a random number until you have found one that isn't in the list.
The way you do it, you generate a new number inside the checking loop. But that doesn't work. Say you're generating the 4th number and find it is equal to the third. Then you generate a new one which might well be equal to any you have already checked against.
You also check uninitialised elements when j > i. Your inner loop should only run up to i.
So:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main()
{
int vec[15] = { 0 };
int i, j;
srand(time(NULL));
for (i = 0; i < 15; i++) {
int okay = 0;
while (!okay) {
vec[i] = rand() % 25 + 1;
okay = 1;
for (j = 0; j < i; j++) {
if (vec[i] == vec[j]) okay = 0;
}
}
printf("%d\n", vec[i]);
}
return 0;
}
That still looks a bit awkward with that okay variable. In my opinion, checking for duplicates should be a separate function:
int contains(int arr[], int n, int x)
{
while (n--) {
if (arr[n] == x) return 1;
}
return 0;
}
int main()
{
// snip ...
for (i = 0; i < 15; i++) {
do {
vec[i] = rand() % 25 + 1;
} while (contains(vec, i, vec[i]));
printf("%d\n", vec[i]);
}
// snip ...
}
In your case the range of possible numbers isn't mich bigger than the number of array elements. You could also create an ordered array {1, 2, 3, ..., 25}, then shuffle it and use only the first 15 elements.
Reset j in the while loop:
for (j=0;j<i;j++){ //Use j<i
if (i!=j){
while(vec[i]==vec[j]){
vec[i]=rand() % 25+1;
j=-1;//-1 because in the next iteration,j will start from 0
}
}
}
Are you actually trying to shuffle the numbers, rather than fill the array with randoms? (It looks like you want an array with numbers from 1 to 25, but in random order.) rand() can give you duplicate numbers (they're random, after all!)
Try this:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int
main( int argc, char **argv )
{
int i, vec[25];
for (i = 0; i < 25; ++i) vec[i] = i + 1;
/* Shuffle entries */
srand( time( 0 ) );
for (i = 0; i < 1000; ++i) {
int a = rand( ) % 25;
int b = rand( ) % 25;
if (a != b) {
int tmp = vec[a];
vec[a] = vec[b];
vec[b] = tmp;
}
}
/* Print shuffled array */
for (i = 0; i < 25; ++i) printf( "%d: %d\n", i, vec[i] );
return 0;
}
#include<stdio.h>
#include<stdlib.h>
int inArray(int, int, int*);
int main()
{
int i,j,vec[15]={0};
int temp;
srand (time(NULL));
for (i=0;i<15;i++){
temp =rand() % 25+1;
while(inArray(i+1,temp, vec) == 1){
temp = rand() % 25+1;
}
vec[i] = temp;
printf("VECT[%d] \t= %d\n",i,vec[i]);
}
return 0;
}
int inArray(int count, int input, int* array){
int i = 0;
for(i=0; i<count; i++){
if(input == array[i]){
return 1;
}
}
return 0;
}
Gave an output:
VECT[0] = 24
VECT[1] = 19
VECT[2] = 1
VECT[3] = 25
VECT[4] = 22
VECT[5] = 18
VECT[6] = 7
VECT[7] = 8
VECT[8] = 12
VECT[9] = 21
VECT[10] = 11
VECT[11] = 6
VECT[12] = 23
VECT[13] = 20
VECT[14] = 15
The checking was off, you would change and not break allowing it to be changed to a previous value.
You can use an array
int randNumbers[25]; // fill it starting 0 to 25 then
randomize the number in a range between 0 and 25 after swap the number in the randomized index with the last number in your array
randomize 0 to 23
and so on....
int main(int argc, char **argv) {
static const int size = 25;
int numbers[size];
for( int i = 0; i < size; i++ ){
numbers[i] = i;
}
srand (time(NULL));
for( int i = 0; i < size; i++ ){
int rIndex = rand()%(size - i);
int rNum = numbers[rIndex];
numbers[rIndex] = numbers[size-i];
printf("%d ", rNum);
}
return 0;
}
O(n) complexity...