Finding the maximum value in a matrix (C programming) [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 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.

Related

Why in C without a semicolon gives an error? [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 4 months ago.
Improve this question
Why does it throw an error in the seventh round without semicolons (;). When do you need to put such semicolons in the code?
#include <stdio.h>
#define S 10
void print_arr(int *arr, int size) {
int *p = arr + S;
for(;arr < p; arr++) {
printf("%d ", *arr);
}
}
int main() {
int arr[S] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
print_arr(arr, S);
return 0;
}
if you try without a semicolon (;) then it gives the following error
main.c:7:23: error: expected ';' before ')' token
7 | for(arr < p; arr++) {
| ^
| ;
A for loop consists of four parts:
for(init;condition;iteration) { body }
For the compiler to know what is used as init, condition or iteration, you separate it via semicolon. if you want to keep one of the parts empty, you simply write a semicolon.
for(;i < 10;i++) is a loop that has no initialisation. only a condition and an iteration statement.
for(int i= 0;; i++) has no condition and
for(int i=0;i < 10;) has no iteration statement.
writing for(i < 10; i++) is simply not valid syntax.

Sorting an array - C language [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
I have tried to write a sorting function sort(int *buffer, int array[], int size) which works in a way similar to the insertion sort - it takes the first element from the array, sets it as the first element of the buffer and then checks whether or not the next value showing up in the array is greater than the last value stored in the buffer. If yes, it keeps swapping the two elements until everything is in its place. This is my minimal working example:
#include <stdio.h>
void sort(int *buffer, int array[], int size) {
for(int i = 0; i < size; i++) {
buffer[i] = array[i];
while(i >= 1 && buffer[i] < buffer[i-1]) {
int tmp = buffer[i-1];
buffer[i-1] = buffer[i];
buffer[i] = tmp;
printf("i = %d i: %d, i -1 : %d \n",i, buffer[i], buffer[i-1]);
i--;
}
}
}
int main(void) {
int array[3] = {4,3,2};
int buffer[3];
sort(buffer, array, 3);
for(int i = 0; i < 3; i++) {
printf("%d", buffer[i]);
}
}
However, the output of this program is 222
To be honest, I don't see how it's even possible that three identical elements got placed in the buffer.
What can have gone wrong?
You are using the same variable for the inner while cycle and for the outer for loop. Use a different variable and copy the value of i to it in each iteration of the for.

Why are the wrong array values being printed? [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 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.

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.

C Programm output completely changed due to unrelated, empty printf statement [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 6 years ago.
Improve this question
I have this C program code here, which determines the highest contiguous value for an array:
#include <stdio.h>
int inputs[8];
int calcSum(int i, int j);
int main(void) {
int i, j, maxSum = 0, tempSum = 0;
int length = sizeof(inputs)/sizeof(inputs[0]);
for(i=0;i<length;i++) {
scanf("%d", &inputs[i]);
}
for(i=0;i<length;i++) {
for(j=i;j<length;j++) {
tempSum = calcSum(i,j);
if(tempSum > maxSum) {
maxSum = tempSum;
}
}
}
printf("%d\n", maxSum);
return 0;
}
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
Even though this code looks correct to me, it outputs a wrong result. Sampling adding any sort of printf("") (can be empty as well) between tempSum = ... and if(tempSum >...) will make the code output the correct answer for all test cases. I even rewrote the entire code from scratch and still get the same issue.
For example, the number series: 5 2 -1 -2 -4 3 5 -6 should output 8, which it does once the printf("") is added, otherwise it outputs 38...and I have no idea why. Can you please explain, where I went wrong?
In this code:
int calcSum(int i, int j) {
int c, sum;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}
You need to make sure you initialize sum to a starting value:
int calcSum(int i, int j) {
int c, sum;
sum=0;
for(c=i;c<=j;c++) {
sum+=inputs[c];
}
return sum;
}

Resources