Method returning the wrong value - c

I'm learning programming in C and C#. This C code is not computing as expected. The method returns the value: -504476904 to the console? What can be the problem here?
Here is the code:
#include <stdio.h>
#include <stdlib.h>
int minimum(values)
int values[10];
{
int minimum_value, i;
minimum_value = values[0];
for (i = 1; i < 10; i++)
{
if(values[i] < minimum_value)
{
minimum_value = values[i];
}
}
return (minimum_value);
}
int main()
{
int sumthing[10];
int i;
for (i = 1; i < 10; i++)
{
sumthing[i] = (i * 34);
}
printf("Minimum value of for loop is: %d\n", minimum(sumthing));
return 0;
}
I changed my code to this:
#include <stdio.h>
#include <stdlib.h>
int minimum(values)
int values[10];
{
int minimum_value, i;
minimum_value = values[0];
for (i = 1; i < 10; i++)
{
if(values[i] < minimum_value)
{
minimum_value = values[i];
}
}
return (minimum_value);
}
int main()
{
int sumthing[10];
int i;
for (i = 0; i < 10; i++)
{
sumthing[i] = ((i + 1 )* 34);
}
printf("Minimum value of for loop is: %d\n", minimum(sumthing));
return 0;
}

sumthing[0] is not initialized in your code, since you start looping at index 1 ; that is a first step to correct your program, in the mainfunction :
for (i = 0; i < 10; i++)
{
sumthing[i] = (i * 34);
}
BTW, you're using K&R style. This is considered obsolete.
A more modern implementation of your code, with the above correction ; it seems to be working :
#include <stdio.h>
int minimum(int values[])
{
int minimum_value, i;
minimum_value = values[0];
for (i = 1; i < 10; i++)
{
if(values[i] < minimum_value)
{
minimum_value = values[i];
}
}
return (minimum_value);
}
int main()
{
int sumthing[10];
int i;
for (i = 0; i < 10; i++)
{
sumthing[i] = (i * 34);
}
printf("Minimum value of for loop is: %d\n", minimum(sumthing));
return 0;
}

This syntax is very, very archaic:
/* Don't do this */
int minimum(values)
int values[10];
{
...
/* Do this instead */
int minimum(int values[10])
{
...
Other than that, I looks fine.
I copied/pasted your code, changed "minimum()" to int minimum(int values[10]), ran it ... and got the correct result "34":
Minimum value of for loop is: 34

Related

why the output of this program is wrong?.Frequency of number

Program for the frequency of a number
Please help me with this code to get clear output. I am a beginner
I have made the program using an array. I don't know whether it is correct or not. Made with my own logic
int count(int a)
{
int c;
while(a>=1)
{
c++;
a=a/10;
}
return c;
}
int main()
{
//program to find frquency of the number
int a,n,d;
int b[100];
int e[100];
scanf("%d",&a);
n=count(a);
for(int i=n;a>0;i--)
{
b[i]=a%10;
a=a/10;
}
for(int i=1;i<=n;i++)
{
d=b[i];
e[d]++;//most probably this part error occurs
printf("%d\n",d); //used this this to confirm that i have correctly stored value in d.
}
for(int i=1;i<=n;i++)
{
printf("%d ",e[i]);
}
return 0;
}
The line int c; should be int c = 0;
The line int e[100]; should be int e[100] = {0};
The following code could work:
#include <stdio.h>
int count(int a) {
int c = 0;
while (a >= 1) {
c++;
a = a / 10;
}
return c;
}
int main() {
// program to find frquency of the number
int a, n, d;
int b[100];
int e[100] = {0};
scanf("%d", &a);
n = count(a);
for (int i = n; a > 0; i--) {
b[i] = a % 10;
a = a / 10;
}
for (int i = 1; i <= n; i++) {
d = b[i];
e[d]++; // most probably this part error occurs
printf("%d\n", d); // used this this to confirm that i have correctly
// stored value in d.
}
for (int i = 1; i <= n; i++) {
printf("%d ", e[i]);
}
return 0;
}
Also, you can do it use snprintf:
#include <stdio.h>
int main() {
int a;
int max = -1;
char buf[100];
int count[10] = {0};
scanf("%d", &a);
snprintf(buf, sizeof(buf), "%d", a);
for (int i = 0; buf[i] != '\0'; ++i) {
int temp = buf[i] - '0';
++count[temp];
if (temp > max)
max = temp;
}
for (int i = 0; i <= max; ++i)
printf("%d ", count[i]);
return 0;
}

Need to create 2 arrays with random numbers and then sort both of them in C

I have a little problem. I need to create 2 arrays. First I have to sort each of them and then I have write them in sorted order in main function . Any way? I am hopeless..
I Have no idea how to declare array in function. So I can call the sort function from main.
Here is my program
int array_a()
{
int i,switchh,switching;
int howmany = 10;
int a[howmany];
for (i=0;i<howmany;i++)
{
a[i] = rand() % 10+1;
}
for (i=0; i<howmany;i++)
while(1){
switchh=0;
for (i=0; i<howmany-1;i++){
if (a[i]>a[i+1]){
int switching=a[i];
a[i]=a[i+1];
a[i+1]=switching;
switchh = 1;
}
}
if(switchh==0)
{
break;
}
}
}
int array_b()
{
int i,switchh,switching;
int howmany = 10;
int b[howmany];
for (i=0;i<howmany;i++)
{
b[i] = rand() % 10+1;
}
while(1){
switchh=0;
for (i=0; i<howmany-1;i++){
if (b[i]>b[i+1]){
int switching=b[i];
b[i]=b[i+1];
b[i+1]=switching;
switchh = 1;
}
}
if(switchh==0)
{
break;
}
}
}
main()
{
return 0;
}
To write a sort function declare a function like void sort(int *arr, int count), pass the array to this function. Note that you should call srand() at once at the beginning of the program to get a better random distribution. Example:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
void sort(int arr[], int howmany)
{
int i, switchh;
while(1)
{
switchh = 0;
for(i = 0; i < howmany - 1; i++)
{
if(arr[i] > arr[i + 1])
{
int switching = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = switching;
switchh = 1;
}
}
if(switchh == 0)
break;
}
}
int main(void)
{
srand((unsigned)time(0));
int howmany = 10;
int arr[howmany];
int i;
for(i = 0; i < howmany; i++)
arr[i] = rand() % 10 + 1;
for(i = 0; i < howmany; i++)
printf("%2d, ", arr[i]);
printf("\n");
sort(arr, howmany);
for(i = 0; i < howmany; i++)
printf("%2d, ", arr[i]);
printf("\n");
return 0;
}

C int array issue

my output values
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define W 160
#define H 105
struct dungeons {
int x;
int y;
int width;
int height;
};
void randomNumberGenerator(int seed);
void makeGameBoard(int gameBoard[W][H]);
void makeDungeonRooms(struct dungeons d[10]);
void printGame(int gameBoard[W][H]);
int main(int argc, char *argv[]) {
int gameBoard[W][H];
//int gameBoard[160][105] = {2};
struct dungeons d[10];
makeGameBoard(gameBoard);
makeDungeonRooms(d);
printGame(gameBoard);
return 0;
}
void randomNumberGenerator(int seed) {
srand(seed);
}
void makeGameBoard(int gameBoard[160][105]) {
int i;
int j;
for (j = 0; j < 105; j++) {
for (i = 0; i < 160; i++) {
gameBoard[i][j] = 2;
//gameBoard[i][j] = 2;
/*
if (j == 0) {
gameBoard[i][j] = 2;
} else
if (j == 104) {
gameBoard[i][j] = 2;
} else
if (i == 0) {
gameBoard[i][j] = 2;
} else
if (i == 159) {
gameBoard[i][j] = 2;
} else {
gameBoard[i][j] = 4;
}
*/
}
}
gameBoard[0][1] = 2;
gameBoard[0][0] = 2;
gameBoard[159][0] = 3;
}
void makeDungeonRooms(struct dungeons d[10]) {
int i;
for (i = 0; i < W; i++) {
d[i].x = 0;
d[i].y = 0;
d[i].width = 0;
d[i].height = 0;
}
}
void printGame(int g[W][H]) {
int i;
int j;
for (i = 0; i < W; i++) {
for (j = 0; j < H; j++) {
printf("%d", g[i][j]);
/*
if (g[i][j] == 2) {
printf("|");
} else
if (g[i][j] == 1) {
printf("X");
} else {
printf(" ");
}
*/
}
printf("\n");
}
}
I'm making a grid. But the first 5 rows and half of the 6th row, when printed output the value 0. I'm setting everything to 2 in the for loop, and even after when I try to manually change the value, it still doesn't. The last one [159][0] works but nothing in the first 5 rows and half of the 6th seem to work. They stay 0. Why?
The problem is here:
void makeDungeonRooms(struct dungeons d [10]){
int i;
for(i = 0; i< W; i++){
d[i].x = 0;
d[i].y = 0;
d[i].width = 0;
d[i].height = 0;
}
}
where #define W 160. Accessing array out of bound can trigger undefined behavior. It can trigger a segmentation fault, it can remain unnoticed for a long time or alter the output of the program in very strange ways. Here, zeroing the dungeons also partly zeroed the gameBoard. Keep it that way: your game is going to be hilarious!

how to generate number pattern in triangular form [duplicate]

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;
}

How do I generate number pattern in triangular form

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;
}

Resources