Why are the wrong array values being printed? [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
For some reason, the nested for loop I have created at the bottom seems to be printing out the wrong first value (Gives me 3, when it should be 8). Yet, when I simply do printf (at the bottom), I am given the right value. Not really sure what's wrong with my code.
#include <stdio.h>
int main(void)
{
int d;
printf("Please input dimensions: (between 3 and 9, inclusive): \n");
scanf("%i", &d);
int array[d][d];
int k = 1;
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
array[i][j] = (d * d) - k; //d^2 doesn't work to square a function
k++;
}
}
for (int z = 0; z < d; z++)
{
for (int y = 0; y < d; y++)
{
printf("%i\n", array[z][y]);
}
}
printf("%i\n", array[0][0]);
printf("%i\n", array[0][1]);
}
Edit: Sorry guys, the top value that was being printed was my own input. I was simply thinking it was the first value being printed.

Are you sure you aren't looking at your own input? I cut and paste and see:
Please input dimensions: (between 3 and 9, inclusive):
3
8
7
6
5
...
The 3 is actually what I typed and is echoed.

Related

Not getting the correct output elements from an array in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I'm trying to write a code that inputs a number each time into an array and than print out the result from the arrays elements but for some reason i either get an infintie loop or it prints out the same number.
void main() {
char arr[SIZE];
int k = 1;
int i = 0;
while (k != 0) {
scanf("%d", &k);
arr[i] = k;
i++;
}
arr[i] = '\0';
int b = 0;
while (b < i) {
printf("elements are %d\n", arr[i]);
b++;
}
You want to print arr[b] and not arr[i].
Thus, you want: printf("elements are %d\n", arr[b]);
As you [currently] have it, printing arr[i] will always print the same element and it's UB because at that point i is one beyond the end of the arr array, so the value will be unknown/undefined.

Is my code wrong or it's somehow compiler's fault? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
stdout gives:--------------------------------------------------
Here are the results of all 3 students.--------------------------------------------------
Although I wanted it to display:--------------------------------------------------Here are the results of all 3 students:
--> 8.75
--> 9.25
--> 9.00 --------------------------------------------------
Here is the code:
#include <stdio.h>
int main(void) {
/*The grades of 3 students*/
int grades[3][4] = {{ 7,10,9,9 }, { 10,10,8,9 }, { 9,8,9,10 } };
float result1 = (grades[0][0] + grades[0][1] + grades[0][2] + grades[0][3])/4;
float result2 = (grades[1][0] + grades[1][1] + grades[1][2] + grades[1][3])/4;
float result3 = (grades[2][0] + grades[2][1] + grades[2][2] + grades[2][3])/4;
printf("Here are the results of all 3 students:\n");
/*I stored the results into an array so i can use for loop to display them faster*/
float a[3]= {result1,result2,result3};
/*Here i wanted to try to display the results using for loop*/
int i;
for (i = 0; i > 3; i++){
printf(" --> %.2f\n", a[i]);
}
return 0;
}
What's the problem with the output ?
The problem is here:
for (i = 0; i > 3; i++)
Try for (i = 0; i < 3; i++) (while i is less than 3, instead of while i is greater than 3, which is never...)

Prime number into an array in C [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I try to do a function in order to sort an array and display after that only the prime number. But all the elements of my array are random numbers, and the problem is that the function display only negative prime numbers and not positive like 7 and 3, what can I do in order to solve the problem
int prime_arr(int size, int *arr, int *sort_arr)
{
int i, j, k = 0, flag;
for (i = 0; i < size; i++)
{
flag = 0;
for (j = 2; j < arr[i]/2; j++)
{
if (arr[i] % j == 0){
flag = 1;
break;
}
}
if (flag == 0){
sort_arr[k++] = arr[i];
}
}
return j;
}
I see 3 problems with the code:
1. You should return k, not j. k is the size of sort_arr
2. You should loop until arr[i] / 2, not one less than that (see <= in code below)
3. You do not handle negative numbers. Change your loop to the following:
for (j = 2; j <= abs(arr[i])/2; j++)
Without the code that prints your values, I'm not sure exactly what you're looking for, but hopefully fixing these will fix your problem.

The int array displays an odd value at the end [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am running this code for teaching purposes about sizeof and displaying array values; everything looks OK with the char array (the last element of the array '0' or NULL is not displayed, the float array displays it as 0.0000... but the char array gives me the last element of 54417138 that looks like an address (hex equivalent 206F6554). I tried to retrieve the content of it assuming it is an address (see the comment part of the code) but got a stack overflow error so I am lost at this moment - any help will be appreciated.
#include <stdlib.h>
int main(void) {
int i =0;
int j = 0;
int k = 0;
char name[] = {'T','e','o',' ','G','u','g' };
int z = sizeof(name)/sizeof(name[0]);
printf("%d\n", z);
for ( k=0; k <= z; k++) {
printf("%c", name[k]);
}
for ( k=0; k <= z; k++) {
printf("%c", *(name+k));
}
printf("\n");
int number[] = {1,2,3,4,5,6,7,8 };
int x = sizeof(number)/sizeof(number[0]);
printf("%d\n", x);
for ( i=0; i <= x; i++) {
printf("%d\t", number[i]);
}
for ( i=0; i <= x; i++) {
printf("%d\t", *(number+i));
}
printf("\n");
/*int *ptr = 0x206F65540;
printf("Memory address is: %i\n", ptr);
printf("Content of that address is: %d\n", *ptr);
printf("\n");*/
float number1[] = {1.0, 2.5, 3.1234, 10.10};
int y = sizeof(number1)/sizeof(number1[0]);
printf("%d\n", y);
for (j=0; j <= y; j++) {
printf("%f\t", number1[j]);
}
for (j=0; j <= y; j++) {
printf("%f\t", *(number1+ j));
}
printf("\n");
return EXIT_SUCCESS;
}
This is the output of the program:
7
Teo Gug Teodor Gug
8
1 2 3 4 5 6 7 8 544171348 1 2 3 4 5 6 7 8 544171348
4
1.000000 2.500000 3.123400 10.100000 0.000000 1.000000 2.500000 3.123400 10.100000 0.000000
note the strange value in the display of int array of 544171348*
You are accessing beyond the end of your arrays with this kind of loop
for ( i=0; i <= x; i++) {
should be
for ( i=0; i < x; i++) {
^
^
as array indices are zero based.
Reading beyond the end of your arrays is undefined behaviour.

Finding the maximum value in a matrix (C programming) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I'm learning to code in C. I've wrote the following code to find the maximum value inside a matrix, but for some reason the program would return the highest value in the first row (87), except the desired 99. I can't find the flaw in the code. Would be really happy for some help!
#include <stdio.h>
int Maxmin(int a[][4], int row, int col) {
int i, j, max;
max = a[0][0];
for (i = 0; i < row; i++) {
for (j = 0; j < col; j++) {
if (a[i][j] > max)
max = a[i][j];
}
return max;
}
}
void main() {
int a[3][4] = {
{ 3, 87, 11, 23 },
{ 99, 78, 19, 44 },
{ 59, 60, 13, 14 }
};
int num;
num = Maxmin(a, 3, 4);
printf("%d\n", num);
}
Not really easy to spot as usually bugs like this are down to i & j typos.
The return max; is inside the row's for loop.
Move it to the end of the function and then you'll examine every row of the matrix. I actually compiled and ran this fix.
The way to discover such errors, is to either put in extra print statements, like :
printf( "a[%d][%d]=%d ", i, j, a[i][j]);
Or to step through your program with a debugger, setting a break point on the code you're interested in.

Resources