What is wrong with this lottery Input - Raffle code?
I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2 sets of 7 numbers (mine and the random one) are compared, and the program outputs how much of my numbers are the same with the randomley picked ones. (the order of numbers doesn't matter) If I input more than one same numbers, for expample, 1 1 2 3 4 5 6, or input a number larger than 45, an error message must be printed. If both of these errors are true, there should be a seperate error message.
What is wrong with this lottery Input - Raffle code?
I have been working on a code that when I input 7 numbers by scanf (1~45 integer), 7 numbers are randomley picked, the 2
When I run the program, the radom picking of numbers (raffle) seems to be working fine, no overlapping numbers. the counting of matching number works as well. what is weird is the error messages. since the counting works, I assume the input values are well saved, but the program always outputs multiple lines of (7, exactly) "You cannot choose same number" . can anyone help me with this problem? The source code is below.
#include <stdio.h>
#include <stdlib.h>
int main () {
int yours[7];
printf("Buy yours: ");
for (int i=0; i<7; i++){
scanf("%d", &yours[i]);
for (int j=0; j<7; j++){
for (int k=0; k<7; k++){
if (yours[j] == yours[k] && yours[k]>45){
printf("You cannot choose same number and number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
}
else if (yours[j]>45){
printf("You cannot choose number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
break;
}
else if (yours[j] == yours[k]){
printf("You cannot choose same number.");
printf("\n\n");
printf("Buy yours: ");
break;
}
break;
}
break;
}
}
printf("Lottery result: ");
int lottery[7];
for (int i=0; i<7; i++){
lottery[i] = rand() %45 + 1;
for (int j=0; j<i; j++){
if (lottery[i] == lottery[j]) {
i--;
break;
}
}
}
for (int k=0; k<7; k++){
printf("%d ", lottery[k]);
}
int a = 0;
for (int i=0; i<7; i++){
for (int j=0; j<7; j++){
if (lottery[i] == yours[j]) {
a++;
}
}
}
printf("\n");
printf("The number of yours : %d", a);
return 0;
}
As other have already said, the code is a mess, since there's no identation, so it's quite difficult to understand and there's a high risk of making errors in the code.
Check out here: https://codehs.gitbooks.io/introcs/content/Programming-with-Karel/how-to-indent-your-code.html
Did you use something like the old version of Dev C++ to write the code? I've seen others struggling with that.
I would also suggest you to not specify array size as a number: e.g int array[7]; but to define a constant int this way:
#define array_size 7
int array[array_size];
This will let you to modify your program without the need to rewrite all the code and also will prevent you to have some bugs, and you should give self describing names to your variables.
#include <stdio.h>
#include <stdlib.h>
int main ()
{
int yours[7];
printf("Buy yours: ");
for (int i=0; i<7; i++)
{
scanf("%d", &yours[i]);
I think that the problem is here, since you are comparing values that still don't exist, the second for cycle and the third one are executed before the array yours if fully initialized. To fix it you have to change their conditions
for (int j=0; j<7; j++)
{
for (int k=0; k<7; k++)
{
if (yours[j] == yours[k] && yours[k]>45)
{
printf("You cannot choose same number and number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
}
else if (yours[j]>45)
{
printf("You cannot choose number out of range from 1 to 45.");
printf("\n\n");
printf("Buy yours: ");
break;
}
else if (yours[j] == yours[k])
{
printf("You cannot choose same number.");
printf("\n\n");
printf("Buy yours: ");
break;
}
break;
}
break;
}
}
printf("Lottery result: ");
int lottery[7];
for (int i=0; i<7; i++)
{
lottery[i] = rand() %45 + 1;
for (int j=0; j<i; j++)
{
if (lottery[i] == lottery[j])
{
i--;
break;
}
}
}
for (int k=0; k<7; k++)
{
printf("%d ", lottery[k]);
}
int a = 0; //What does it mean?
for (int i=0; i<7; i++) //Also check these
{
for (int j=0; j<7; j++)
{
if (lottery[i] == yours[j])
{
a++;
}
}
}
printf("\n");
printf("The number of yours : %d", a);
return 0;
}
Related
My program should ask user how many numbers he wanna input in the array , and than input one by one. Any number that's higher than 99 should be replaced by '0'.
That part works fine. But in the end created array should be placed in the table which needs to be in format (5 columns / depending rows).
This is what I wrote:
#include <stdio.h>
#define MAX 80
int main()
{
int n = 0;
int i,j;
int field[MAX]={0};
printf("how many numbers do u want to input? ");
scanf("%d",&n);
for ( i = 0; i < n; i++)
{
printf("Input number %d: ",i+1);
scanf("%d",&field[i]);
if(field[i] / 100 >= 1 )
{
field[i] = 0;
}
}
for ( i = 0; i<n; i++)
{
printf("\nfield[%d] = %d\n", i, field[i]);
}
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
return 0;
}
This part of your code can not be output in five columns
for(i=0; i <= 5;i+=5)
{
for(j =i; j <n;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
So if you change this, it will be executed
for(i=0; i <n;i+=5)
{
for(j =i; j <i+5;j++)
{
printf("%d ",field[j]);
}
printf("\n");
}
I hope this code will be help.
if i am wrong, please tell me and give a chance.
I want to help you and be recognized.
I have a one-dimensional array in the C language. I enter the number of elements (n) and then I enter the elements themselves. I want to find the position and the value of the first positive element and of the last negative element. How can I do it? I was thinking about something like this, but there's a semantic error and I'm stuck. I am just a beginner and I would really appreciate your help. Thank you in advance!
#include <stdio.h>
int main()
{
int Array[100], i, n;
printf("Enter n: ");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (i = 0; i < n; i++)
{
printf("Enter number%d: ", i + 1);
scanf("%d", &Array[i]);
}
for (i = 0; i < n; i++)
{
if (Array[i] > 0)
{
printf("The first positive element is %d\n", Array[i]);
}
}
for (i = 100; i < n; i--)
{
if (Array[i] < 0)
{
printf("The last negative element is %d\n", Array[i]);
}
}
}
Test for first positive does not exit so you'll get all of them.
Test for negative needs to start at n-1 not 100 and terminate after it has tested element 0. It, too, needs to break when it's found one.
In the line:
for (i=100; i<n; i--)
you should write
for (i=(n-1); i>=0; i--)
that way you start from the last inserted element until you reach 0, the beggining of your array.
Furthermore in your if's you could use a break; command after your printf's. That way when you meet an element with the required characteristics you exit your loop. If you don't use break, you print every positive and negative element, not just the first you meet
Few problems:
in your 2nd loop - you don't break, meaning it will continue running (and printing!!) even after 1st positive was found
in your 3rd loop - i = 100; i < n. Wrong! it should have been i = n-1; i >= 0 since the array is only 100 elements long. And also the same problem with no break as before
INDENTATION!!!! (makes your code readable)
What happens if n > 100 ? (hint: you will insert elements to memory you don't own --> UB)
Here is a fixed version:
#include <stdio.h>
int main()
{
int Array[100], i, n;
printf("Enter n: ");
scanf("%d", &n);
if( n > 100)
{
printf("error\n");
return 1;
}
printf("Enter %d elements\n", n);
for(i=0; i<n; i++)
{
printf("Enter number%d: ",i+1);
scanf("%d", &Array[i]);
}
for(i=0; i<n; i++)
{
if (Array[i]>0)
{
printf("The first positive element is %d\n", Array[i]);
break;
}
}
for (i=n-1; i>=0; i--)
{
if (Array[i]<0)
{
printf("The first negative element is %d\n", Array[i]);
break;
}
}
return 0;
}
I am trying to make a program that generates two matrices of the same number of lines and columns (given by the user. numbers on the matrices are also generated) and then another matrix that is filled with numbers given by the user.
For example, if I ask for matrices with 3 lines and 3 columns, the program sometimes works fine and sometimes it doesn't. In any of the ways, the program always crashes on the end. I've tried to debug but no clue. Might be the compiler? This is what I have:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int index, i, j, random, k, l;
srand(time(NULL));
printf("How many colums/lines?\n");
scanf("%d", &index);
int(*matrix1)[index]=malloc(sizeof(int[index][index]));
if(matrix1 == NULL)
{
printf("Error\n");
}
for(i=1; i<=index; i++)
{
for(j=1; j<=index; j++)
{
random = rand()%10;
matrix1[i][j]=random;
printf("%d ", matrix1[i][j]);
}
printf("\n");
}
printf("\n\n");
int(*matrix2)[index]=malloc(sizeof(int[index][index]));
if(matrix2 == NULL)
{
printf("Error\n");
}
for(k=1; k<=index; k++)
{
for(l=1; l<=index; l++)
{
random = rand()%10;
matrix2[k][l]=random;
printf("%d ", matrix2[k][l]);
}
printf("\n");
}
int(*matrix3)[index]=malloc(sizeof(int[index][index]));
printf("Input the numbers for the third matrix.\n");
for(i=1; i<=index; i++)
{
for(j=1; j<=index; j++)
{
printf("Fill the position %d%d\n", i, j);
scanf("%d", &matrix3[i][j]);
}
}
free(matrix1);
free(matrix2);
free(matrix3);
return 0;
If n=3,the output is
1*2*3
7*8*9
4*5*6
If n=5,the output is
1*2*3*4*5
11*12*13*14*15
21*22*23*24*25
16*17*18*19*20
6*7*8*9*10
CODE:
int i, j, a[50][50], k = 1, m = 0;
for (i = 0; i < n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = k;
k++;
}
printf("\n");
}
m = k;
for (i = 1; i <= n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = m;
m++;
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d", a[i][j]);
}
printf("\n");
}
I am not so good in c language, but i think it will help you.
please have a look, and you can do making some change if any syntax error occurs but logic is clear.
#include <stdio.h>
#include <math.h>
void printOutput(int n){
int k = ceil(n/2);
int m =1;
int j =1;
int l =k;
int i;
int b;
for(i=1;i<=n;i++){
for(b=m;b<=m+n;b++){
printf(b);
}
printf("\n");
if(i<k){
j= (2*j);
m =n*j+1;
} else{
int z = n-i-1;
m= n+1 +n*(2)*z;
l =z-2;
}
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
printOutput(input);
}
#include<stdio.h>
#include<conio.h>
int k=1;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n/2+1;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k+n;
}
k=k-3*n;
for(int i=1;i<=n/2;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k-(n/2+1)*n;
}
}
This is a rough sketch of what you should do, there are some minor flaws with it... However, the functionality is there. Next time, if you can't understand what algorithm the code calls for, I suggest you write this array out on a sheet of paper and follow each row. Notice where each row gets placed, you should start to come up with a way to do this (there are more ways than this one...) It may seem hard at first, but if you want to be in this field, you have to have the mindset for it. I agree with the others, this is NOT a homework site, rather a site to help build off the knowledge you know, so begin to actually try to write the program, and then submit it here if you're having trouble with it.
#include <stdio.h>
void loadNprint(int size, int a[][size]){
int i,j,count=1,down=size-1, up =0;
for(i=0; i<size; i++){
for(j=0;j<size; j++){
if((i%2) == 0)a[up][j] = count;
if((i%2) == 1)a[down][j]= count;
count++;
}
if((i%2) == 0)up++;//keeping track of the rows in ascending order
if((i%2) == 1)down--;//keeping track of rows in descending order
}
for(i=0; i<size; i++){
for(j=0; j<size; j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
int myarray[input][input];
loadNprint(input,myarray);
}
This program works perfect. But if you want make some changes..do it yourself.
Next time please try some coding yourself before asking. This will print a different pattern for even numbers.
#include<stdio.h>
#include<conio.h>
int n,beginnew,cpy;
int main()
{
printf("Please enter a value : ");
scanf("%d",&n);
//process
beginnew=n-n/2+1;//beginning of new pattern
cpy=n-1;
for(int i=1;i<n+1;i++)
{
if(i<beginnew)
{
for(int h=n-1;h>=0;h--)
printf("%d * ", (n*(2*i-1)-h) );
}
else
{
for(int h=n-1;h>=0;h--)
printf("%d * ",(n*(cpy)-h) );
cpy=cpy-2;
}
printf("\n");
}
getch();
return 0;
}
//this code print Diagonal Pattern if matrix is
1 2 3
4 5 6
7 8 9
output is :
1
4 2
7 5 3
8 6
9
import java.util.*;
class DiagonalPattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x[][];
int i,j,row,col,p,temp=1,last=0;
System.out.println("how many array wants to create and size of array");
row=sc.nextInt();
col=sc.nextInt();
x=new int[row][col];
System.out.println("Enter " +row*col+ " elements of array of array");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
x[i][j]=sc.nextInt();
last=j;
}
}
for(i=0;i<row;i++)
{
System.out.println("");
int k=i;
for(j=0;j<=i;j++,k--)
{
if(j==col)
{
break;
}
else
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
for(p=x.length;p>0;p--,temp++)
{
System.out.println("");
i=x.length-1;
int k=i;
for(j=temp;j<=last;j++,k--)
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
}
I want to create this program:
A users enters 10 different values. But when a user enters the same value, the program gives a warning. I did something but it not working.
How can I create working algorithm for this program?
#include<stdio.h>
#include<stdlib.h>
int main()
{
int x,i,j;
int dizi[10];
for (i = 0; i < 10; i++)
{
printf("%d. number\n", i + 1);
scanf_s("%d", &dizi[i]);
for (j = 0; j < 10; j++)
{
if (dizi[j] == dizi[i])
{
printf("You have already entered this number");
}
}
}
printf("Entered numbers:\n");
for (i = 0; i < 10; i++)
{
printf("%d\t", dizi[i]);
}
system("pause");
return 0;
}
Change the checking loop to
for (j = 0; j < i; j++)
otherwise the check will check using uninitialised data.
When you are giving the warning decrease i by 1.
Otherwise you will not get 10 different number.
if (dizi[j] == dizi[i])
{
printf("You have already entered this number");
i--;
}
Inner loop with j should only check till i.
for( int j=0; j<i; j++)
for (j = 0; j < i; j++)
{
if (dizi[j] == dizi[i])
{
printf("You have already entered this number\n");
--i;
break;
}
}