I have got some more problems with the code. This program ask the user to specify the nr of throws then it throws 3 dices and add these 3 dices to sum.
Then another function sorts the sum form the smallest to the largest with a bubble sorting algorithm.
the first two functions seems to work but the program does not print out the result of the 3rd sorting function.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX 100
//This function ask the user for the amout of throws
int numberofthrows() {
int throws
printf("Type in the number of throws");
scanf("%d", &throws);
return throws;
}
//This function makes the random throws of 3 dices with regard to the number of throws
int filler(int thrownr, int dice1[MAX], int dice2[MAX], int dice3[MAX], int sum[MAX]) {
int i, nr;
srand(time(NULL));
for(i = 0; i <= thrownr; i++) {
nr = rand()%6;
dice1[i] = nr + 1;
nr = rand()%6;
dice2[i] = nr + 1;
nr = rand()%6;
dice3[i] = nr + 1;
sum[i] = dice1[i] + dice2[i] + dice3[i];
}
int j;
for(j = 0; j <= thrownr; j++) {
printf("%d ", dice1[j]);
printf("%d ", dice2[j]);
printf("%d ", dice3[j]);
printf("%d\n", sum[j]);
}
}
//This function sorts the result in form the sum array
int sorter(int thrownr, int sum[MAX], int sortsum[MAX]) {
int tmp, i, j, k, m;
for(i = 0; i <= thrownr; i++) {
sortsum[i] = sum[i];
}
for(m = 0; m <= 10; m++) {
for(j = 0; j <= thrownr; i++) {
if (sortsum[j] > sortsum[j+1]) {
tmp = sortsum[j];
sortsum[j] = sortsum[j+1];
sortsum[j+1] = tmp;
}
}
}
for(k = 0; k <= thrownr; k++) {
printf("%d\n", sortsum[k]);
printf("%d\n", sum[k]);
}
}
int main(void) {
srand(time(NULL));
int dice1[MAX];
int dice2[MAX];
int dice3[MAX];
int sum[MAX];
int sortsum[MAX];
int numberofthrows2;
numberofthrows2 = numberofthrows();
filler(numberofthrows2, dice1, dice2, dice3, sum);
sorter(numberofthrows2, sum, sortsum);
return 0;
}
The code for sorting is a bit wrong. Change
for(m = 0; m <= 10; m++)
To
for(m = 0; m <= thrownr-1; m++)
And
for(j = 0; j <= thrownr; i++)
To
for(j = 0; j < thrownr-m-1; i++)
To fix it. Also, call srand once at the start of main. Don't call it more than once in a program or you might get the same "random" numbers everytime you run your program.
Related
I've been trying to sort columns in a matrix (the dimensions are m,n <= 10) via the lexicographical order (if the columns share the same element, then we compare the elements in the row beneath etc.) with some additional conditions. I need to use functions to print the matrix, input random integers up to 5 as its elements and finally arrange the matrix. I think I got the printing and random inputs correctly but I can't figure out the sorting. Plus I can't use global variables which I have no idea how to do, since I haven't been shown. An example matrix would be :
#include <stdio.h>
#include <stdlib.h>
int main()
{
int m, n;
int mat[10][10];
void print_matrix()
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
for (i = 0; i < m; ++i)
{
for (k = i + 1; k < m; ++k)
{
if (mat[i][j] < mat[k][j])
{
a = mat[i][j];
mat[i][j] = mat[k][j];
mat[k][j] = a;
}
}
}
}
}
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int(mat[m][n]);
print_matrix(mat[m][n]);
arrange(mat[m][n]);
print_matrix(mat[m][n]);
return 0;
}
Try this solution(will work for input 0-8 only), also used global variables:
There have multiple solutions. but is the easiest one.
I have converted each of the columns as an integer value. then bubble sorted the integers. After sorting. I have then converted the integer value to digits. (You have to know how to convert individual digits to multiple digit integer and multiple digit integers to single-digit.
Note I have added one(1) with each digit. Because the input can be zero(0). if you convert 0 0 2 1 to an integer will be only 21. the first two digits lost. So I have added 1. so 0 0 2 1 will be converted to 1132. I have done (added 1) for each input(deducted 1 after sorting). so it will not affect other inputs. Be careful input have to be from(0 to 8)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int m, n;
int mat[10][10];
int generatedNumber[10];
void print_matrix()
{
printf("The matrix is:\n");
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", mat[i][j]);
}
printf("\n");
}
}
void random_int()
{
int i, j;
srand((unsigned)time(NULL));
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
mat[i][j] = rand() % 5;
}
}
}
void arrange()
{
int i, j, k, a;
for (j = 0; j < n; ++j)
{
int number = 0;
for (i = 0; i < m; ++i)
{
number = number * 10 + mat[i][j] + 1;///Adding one for avoiding zero(0)
}
generatedNumber[j] = number;
}
for(i = 0; i < n; i++)
{
for(j = 0; j < n-i-1; j++)
{
if( generatedNumber[j] > generatedNumber[j+1])
{
// swap the elements
int temp = generatedNumber[j];
generatedNumber[j] = generatedNumber[j+1];
generatedNumber[j+1] = temp;
}
}
}
for(i = 0; i < n; i++)///columwise
{
int generatedColumnvalue = generatedNumber[i];
for(j = m -1; j>= 0; j--)///row wise and fro last vaue to first
{
mat[j][i] = (generatedColumnvalue%10)-1;///removing extra added 1
generatedColumnvalue/=10;
}
}
}
int main()
{
printf("Input the number of rows : ");
scanf("%d", &m);
printf("Input the number of columns: ");
scanf("%d", &n);
random_int();
print_matrix();
arrange();
print_matrix();
return 0;
}
just testing out sorting methods and I've come across selection sort. I've understood the logic behind the selection sort but I don't get the desired result i wish to see from this program. It doesn't seem to sort at all. Could someone tell me where i went wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int StudentCreation(int StudentRecordArray[10]){
for(int i = 0; i < 10; i++){
StudentRecordArray[i] = rand() % 100; //limiting the marks range from 0 - 100
}
}
int SelectionSort(int SelectionSortarray[]) {
int n = 0;
int tmp = 0;
for(int j = 0; j < 10-1; j++){
int TempMinimum = j;
for(int i = j+1; i < n; i++)
if(SelectionSortarray[i] < SelectionSortarray[TempMinimum])
TempMinimum = i;
if(TempMinimum != j){
tmp = SelectionSortarray[j];
SelectionSortarray[j] = SelectionSortarray[TempMinimum];
SelectionSortarray[TempMinimum] = tmp;
}
}
for (int f = 0; f < 10; f++){
printf("Student %d - %d\n", f+1, SelectionSortarray[f]);
}
}
int main() {
int StudentRecord[10];
int MenuChoice;
srand(time(NULL)); //random number seed generator
StudentCreation(StudentRecord);
printf("the unsorted list:\n");
for (int f = 0; f < 10; f++){
printf("Student %d - %d\n", f+1, StudentRecord[f]);
}
printf("\nthe sorted list:\n\n");
SelectionSort(StudentRecord);
return 0;
}
Is it something wrong with where i have swapped?
There is a typo in the sorting function.
Instead of
int n = 0;
there should be
int n = 10;
I have task about printing board where you should input dimension and program will create a board with 2 numbers inside each cell: 1'st number is random from 1-3 and second one should be zero. I can make it only with zero but when i tried to make a random number everything went wrong way..
Maybe anyone knowns what it wrong with it?
Function calls uploading map:
int randfunc(int i, int n);
int uploadmap(int m,int n){
int a[m][n];
int i,j,k;
// time_t t;
//srand((unsigned)time(&t));
//int randnum = rand() % 3 + 1;
for(i = 0; i < m;i++){
printf("+---");
}
printf("+\n");
memset(a,0,sizeof(a));
for(i = 0;i < m;i++){
for(j = 0; j < n;j++){
printf("|%d %d",randfunc(i,n),a[i][j]);
}
printf("|\n");
for(k = 0;k < m;k++){
printf("+---");
}
printf("+\n");
}
return 0;
}
function which calls random numbers from 1 to 3:
int randfunc(int i, int n) {
time_t t;
srand((unsigned) time(&t));
for( i = 0 ; i < n ; i++ ) {
printf("%d\n", rand() % 3 + 1);
}
return 0;
}
Main function :
int main(int argc, const char * argv[]) {
int m,n;
printf("Enter dimension: \n");
scanf("%d %d",&m, &n);
printf("Map has been uploaded %d\n",uploadmap(m,n));
return 0;
}
The following simple code could work:
#include <stdio.h>
#include <stdlib.h>
void uploadmap(int m,int n) {
for (int i = 0;i < m; ++i) {
for(int j = 0; j < n; ++j)
printf("|%d 0", (rand() % 3)+1);
printf("|\n");
}
}
int main() {
int m, n;
printf("Enter dimension: \n");
scanf("%d %d",&m, &n);
uploadmap(m,n);
return 0;
}
I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}
I want to print this pattern like right angled triangle
0
909
89098
7890987
678909876
56789098765
4567890987654
345678909876543
23456789098765432
1234567890987654321
I wrote the following code:
#include <stdio.h>
#include <conio.h>
void main()
{
clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
for(j=1;j<=f;j++,k--)
{
k=i;
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
for(x=1;x<f;x++,z--)
{
z=9;
printf("%d",z);
}
printf("%d/n");
}
getch();
}
What is wrong with this code? When I check manually it seems correct but when compiled gives different pattern
Fairly simple: use two loops, one for counting up and one for counting down. Print literal "0" between the two.
#include <stdio.h>
int main()
{
for (int i = 0; i < 10; i++) {
for (int j = 10 - i; j < 10; j++)
printf("%d", j);
printf("0");
for (int j = 9; j >= 10 - i; j--)
printf("%d", j);
printf("\n");
}
return 0;
}
Like H2CO3's, but since we're only printing single digits why not use putchar():
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j;
for(i = 0; i < 10; ++i)
{
// Left half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
// Center zero.
putchar('0');
// Right half.
for(j = 0; j < i; ++j)
putchar('9' - i + j + 1);
putchar('\n');
}
return EXIT_SUCCESS;
}
Modified Code:
Check your errors:
# include<stdio.h>
# include<conio.h>
int main()
{
// clrscr();
int i,j,x,z,k,f=1;
for ( i=10;i>=1;i--,f++)
{
k=i; // K=i should be outside of loop.
for(j=1;j<=f;j++,k++)
{
if(k!=10)
{
printf("%d",k);
}
if(k==10)
{
printf("0");
}
}
z=9; //z=9 should be outside loop.
for(x=1;x<f;x++,z--)
{
printf("%d",z);
}
printf("\n");
}
//getch();
return 0;
}
You are defining k=i inside the for loop(loop which has j) so every time k gets value of i and thus it always get value of i and prints that value and your another condition(if(k==10)) will never be true because every time k takes value of i and i is less than 10 after first iteration of loop and z=9 inside loop so every time loop is executed it is taking value z=9 so it is printing wrong value.
Here's a C# version:
static void DrawNumberTriangle()
{
for (int line = 10; line >=1; line--)
{
for (int number = line; number < 10; number++)
{
System.Console.Write(number);
}
System.Console.Write("0");
for (int number = 9; number > line - 1; number--)
{
System.Console.Write(number);
}
System.Console.WriteLine();
}
}
I'd suggest renaming your i,j,x,z,k,f variables to ones that have meaning like the one's I used. This helps making your code easier to follow.
Rather than output the mid 0 using printf, why not print it using the loops itself.
The following short and simple code can be used:
int main()
{
int m = 10, n, p;
while(m >= 1)
{
for(n = m; n <= 10; n++)
printf("%d", n % 10);
for(p = n - 2; p >= m; p--)
printf("%d", p );
printf("\n");
m--;
}
return 1;
}
For high throughput (though of questionable merit in terms of clarity):
#include <stdio.h>
int main() {
char const digits[] = "1234567890";
char const rdigits[] = "9876543210";
for (int i = 0; i < 30; ++i) {
int k = i % 10;
fputs(digits + 9 - k, stdout);
for (int j = 9; j < i; j += 10) fputs(digits, stdout);
for (int j = 9; j < i; j += 10) fputs(rdigits, stdout);
fwrite(rdigits, 1, k, stdout);
fputs("\n", stdout);
}
}
#include <stdio.h>
void print(int i){
if(i == 10){
putchar('0');
return ;
} else {
printf("%d", i);
print(i+1);
printf("%d", i);
}
}
int main(void){
int i;
for(i = 10; i>0; --i){
print(i);
putchar('\n');
}
return 0;
}