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;
Related
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;
}
The question i have is similar to some questions answered, but the answer for this in particular wasn't in there. Here is my code
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<4;j++)
{
printf("%d ", A[positive]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
I want to check if the input is negative and add it to the 1D array only then. The question is how do I do that?
General Tips
int main()
{
int i,j,k,sorted; // do not declare loop control variables outside of their respective loops
int A[4][4];
int C[16];
int positive = 0; // for readability's sake please refrain from using long ass variable names for simple control var's
for(i=0;i<4;i++) // idiomatic way is for(int i = 0; i < 4; i++)
{
for(j = 0; j<4; j++) // for(int j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0)
{
C[positive] = A[i][j];
printf("C = %d\n"); // missing argument & redundant since you intend to print them afterwards anyway?
positive++;
}
}
}
for(j=0;j<4;j++) // this loop is supposed to print all positive numbers?
{
printf("%d ", A[positive]); // you are looping through A[0-3][0], which are not necessarily the positive numbers of A.
}
printf("\n");
//printf("Your positive numbers are: ", positive);
Now to your question at hand. What the code tries to do and what you ask from us are two different things. Right now you are adding positive values to your C array, but your question asks about negative values?
Cleaned up code that behaves the way i think you want it to.
int main()
{
int A[4][4];
int C[16];
int k = 0;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 4; j++)
{
printf("A[%d][%d]: ", i, j);
scanf("%d", &A[i][j]);
if (A[i][j] > 0)
{
C[k] = A[i][j];
k++;
}
}
}
printf("positive numbers in matrix A are: ");
for (int i = 0; i < k; i++) // loop from 0 to actual number of positive integers; array size remains at 16 in this case.
{
printf("%d ", C[i]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
int main()
{ int i,j,k,sorted;
int A[4][4];
int C[16];
int positive = 0;
for(i=0;i<4;i++)
{
for(j = 0; j<4; j++)
{
printf("A[%d][%d]: ", i,j);
scanf("%d", &A[i][j]);
if(A[i][j]>0){
C[positive] = A[i][j];
printf("C = %d\n");
positive++;
}
}
}
for(j=0;j<positive;j++)
{
printf("%d ", C[j]);
}
printf("\n");
//printf("Your positive numbers are: ", positive);
printf("\n");
system("pause");
return 0;
}
where you wrong is in the looping for result, you print a 2D array but you just use 1D array, and in the loop is not until 4 but until the positive, and then in the loop, you print A, but the result is C no A
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 made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.
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(" ");
}
}
}
}