I'm having a problem with saving my results to output file. Probably something's wrong with the function, but I can't find it and fix it. Does anybody know what's wrong ?
PROGRAM'S CODE:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int **str;
int *tab;
void read(){
int n = 1;
char help_sign;
FILE *fp = fopen("In0303.txt", "r");
if(fp == NULL){
printf("Blad odczytu z pliku!");
}
while((help_sign = fgetc(fp)) != EOF){
if (help_sign == '\n'){
n++;
}
}
fclose(fp);
}
void gen(){
int i, n;
int **str;
str =(int**)malloc(n*sizeof(int*));
for (i = 0; i < n; i++){
str[i] = (int*)malloc(4*sizeof(int));
}
}
void load_date(){
int i, n;
int **str;
FILE *fp = fopen("In0303.txt", "r");
for (i = 0; i < n; i++){
fscanf(fp, "%d" , &str[i][0]);
fscanf(fp, "%d" , &str[i][1]);
fscanf(fp, "%d" , &str[i][2]);
str[i][3]=0;
}
fclose(fp);
}
void gen_tab(){
int i, n, k = 0;
int **str;
int *tab;
for (i = 0; i < n; i++){
if (str[i][0] > k) k = str[i][0];
if (str[i][1] > k) k = str[i][1];
}
tab = (int*)malloc(k*sizeof(int));
for (i = 0; i < k; i++){
tab[i] = i + 1;
}
}
void sort(){
int i, n, j,tmp;
int **str;
for (i = 0; i < n-1; i++)
for (j = i+1; j < n; j++){
if (str[i][2] > str[j][2]){
tmp = str[i][0];
str[i][0] = str[j][0];
str[j][0] = tmp;
tmp = str[i][1];
str[i][1] = str[j][1];
str[j][1] = tmp;
tmp = str[i][2];
str[i][2] = str[j][2];
str[j][2] = tmp;
}
}
}
int min(int x, int y){
if (x > y) return y;
else return x;
}
int max(int x, int y){
if (x < y) return y;
else return x;
}
void make(){
int i, j, min1, max1, p, q, n, k;
int *tab;
int **str;
for (i = 0; i < n; i++){
if (tab[((str[i][0])-1)] != tab[((str[i][1])-1)]){
str[i][3] = 1;
min1 = min(tab[((str[i][0])-1)], tab[((str[i][1])-1)]);
max1 = max(tab[((str[i][0])-1)], tab[((str[i][1])-1)]);
for (j = 0; j < k; j++){
if (tab[j] == max1)
tab[j] = min1;
}
}
}
}
void save(){
int i, n;
int **str;
int *tab;
FILE *g = fopen("Out0303.txt","w");
for (i = 0; i < n; i++){
if (str[i][3] == 1){
fprintf(g, "%d ", str[i][0]);
fprintf(g, "%d ", str[i][1]);
fprintf(g, "%d\n", str[i][2]);
}
}
fclose(g);
}
int main(){
read();
gen();
load_date();
gen_tab();
sort();
make();
save();
printf("Wyniki zapisane do pliku!\n");
free(str);
free(tab);
return 0;
}
THIS IS INPUT FILE ("In0303.txt"):
9 17
2 3 8 4 9 8
1 3 3 6 4 5 9 6
2 6 4 4 9 1
2 5 3 4 5 3 9 2
4 3 6 1 9 5
5 1 7 5 9 2
6 5 8 4 9 2
1 4 7 4 9 2
1 8 2 6 3 1 4 2 5 5 6 2 7 2 8 2
AND THIS IS WHAT SHOULD BE CREATED IN OUTPUT FILE: ("Out0303.txt"):
17
3 9 [1]
5 6 [1]
4 9 [2]
6 9 [2]
7 9 [2]
8 9 [2]
1 2 [3]
1 8 [4]
Offhand - in your save function, you don't assign to "n"
Expanding a bit. You seem to want to use "n" as a global variable but you are declaring local instances of it so the value isn't retained across the functions. You could either make it global or pass it around.
Adding to DrC answers:
this piece of code:
for (i = 0; i < n; i++){
does very little if you do not set n, you have this mistake in gen(), gen_tab(), load(), save() and pretty much every other function.
Related
I have to make a Floyd Triangle that prints in this order:
7 8 9 10
4 5 6
2 3
1
But currently my code print like this:
1
2 3
4 5 6
7 8 9 10
CODE:
#include <stdio.h>
int main()
{
int n, i, c, a = 1;
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
for (c = 1; c <= i; c++)
{
printf("%d ", a);
a++;
}
printf("\n");
}
return 0;
}
Can someone help me?
Here you are.
#include <stdio.h>
int main(void)
{
while ( 1 )
{
printf( "Enter a non-negative number (0 - exit): " );
unsigned int n;
if ( scanf( "%u", &n ) != 1 || n == 0 ) break;
int width = 0;
for ( unsigned int tmp = n * ( n + 1 ) / 2; tmp != 0; tmp /= 10 )
{
++width;
}
putchar( '\n' );
for ( unsigned int i = 0; i < n; i++ )
{
unsigned int value = ( n - i ) * ( n - i + 1 ) / 2 - ( n - i - 1 );
for ( unsigned int j = 0; j < n - i; j++ )
{
printf( "%*u ", -width, value++ );
}
putchar( '\n' );
}
putchar( '\n' );
}
return 0;
}
The program output might look like
Enter a non-negative number (0 - exit): 10
46 47 48 49 50 51 52 53 54 55
37 38 39 40 41 42 43 44 45
29 30 31 32 33 34 35 36
22 23 24 25 26 27 28
16 17 18 19 20 21
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 4
7 8 9 10
4 5 6
2 3
1
Enter a non-negative number (0 - exit): 0
I solved it by hacking (i.e. trying different things and adjusting the code based on the output):
I started by moving the generation code to a separate function. I then cut and pasted the function, giving it a different name. I then started modifying the for loops.
I went through four versions until I hit the correct one:
#include <stdio.h>
#include <stdlib.h>
void
fwd(int n)
{
int i, c;
int a = 1;
for (i = 1; i <= n; i++) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev1(int n)
{
int i, c;
int a = 1;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", a);
a++;
}
printf("\n");
}
}
void
rev2(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", a);
a--;
}
printf("\n");
}
}
void
rev3(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = i; c >= 1; c--) {
printf("%d ", (c - i) + a);
a--;
}
printf("\n");
}
}
void
rev4(int n)
{
int i, c;
int a = 0;
for (i = 1; i <= n; i++)
a += i;
for (i = n; i >= 1; i--) {
for (c = 1; c <= i; c++) {
printf("%d ", (c - i) + a);
}
a -= i;
printf("\n");
}
}
int
main(int argc,char **argv)
{
int n;
--argc;
++argv;
if (argc > 0)
n = atoi(*argv);
else
scanf("%d", &n);
printf("fwd:\n");
fwd(n);
printf("\nrev1:\n");
rev1(n);
printf("\nrev2:\n");
rev2(n);
printf("\nrev3:\n");
rev3(n);
printf("\nrev4:\n");
rev4(n);
return 0;
}
Here's the program output:
fwd:
1
2 3
4 5 6
7 8 9 10
rev1:
1 2 3 4
5 6 7
8 9
10
rev2:
10 9 8 7
6 5 4
3 2
1
rev3:
10 8 6 4
6 4 2
3 1
1
rev4:
7 8 9 10
4 5 6
2 3
1
The other answers are great, but no one posted a recursive function so I thought I'd add one.
#include <stdio.h>
void recursive(int n, int i);
void straight(int n);
// one way of doing it with a recursive function
void recursive(int n, int i) {
if(n <= 0)
return;
if(i <= n) {
printf( "%-3d", (((n * (n + 1) / 2) - n) + i) );
recursive(n, i + 1);
} else {
printf("\n");
recursive(n - 1, 1);
}
}
// straightforward nested loop way
void straight(int n) {
int i, j, row_sum;
for(i = n; i > 0; --i) {
// the sum: i + (i-1) + (i-2) + ... + 2 + 1 = (i * (i+1)) / 2
row_sum = (i * (i + 1)) / 2;
for(j = i; j > 0; --j) {
printf("%-3d", row_sum - j + 1);
}
printf("\n");
}
}
// entry point
int main(int argc, char **argv) {
int n = 0;
scanf("%d", &n);
printf("Recursive Output for n=%d\n", n);
recursive(n, 1);
printf("\nStraight Output for n=%d\n", n);
straight(n);
return 0;
}
Output:
Recursive Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
Straight Output for n=5
11 12 13 14 15
7 8 9 10
4 5 6
2 3
1
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
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
int size=10, numbers[size], i, order;
for (i=0; i<10; i++)
{
printf("please enter a number:");
scanf("%d", &numbers[i]);
}
ascending(numbers[], size);
}
void ascending(int numbers[], int size)
{
int temp, i, sflag, count=0;
do
{
sflag = 0;
for(i=1; i <10; i++)
{
if (numbers[i-1] > numbers[i])
{
temp = numbers[i-1];
numbers[i-1] = numbers[i];
unmbers[i] = temp;
sflag = 1;
}
}
count++;
}while(sflag);
for (i=0; i<10; i++)
{
printf("%d\t", numbers[i]);
}
}
the code fails at the the first if statement in the function, it says segmentation error.
im not sure why, i think there may be an error in how i am passing the array to the function.
There are at least two typos in your program.
The first one is in this statement
ascending(numbers[], size);
^^^
There should be
ascending(numbers, size);
The second one in this statement
unmbers[i] = temp;
^^^^^^^^
There should be
numbers[i] = temp;
Also in this statement within the function
for(i=1; i <10; i++)
you are using a magic number 10 instead of the variable size.
Nevertheless your function is inefficient because the inner loop always iterate from 1 to size.
A more efficient its implementation can look as it is shown in the demonstrative program below.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubble_sort( int a[], size_t n )
{
for ( size_t last = n; !( n < 2 ); n = last )
{
last = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( a[i] < a[i-1] )
{
int tmp = a[i];
a[i] = a[i-1];
a[i-1] = tmp;
last = i;
}
}
}
}
int main(void)
{
enum { N = 10 };
int a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
for ( size_t i = 0; i < N; i++ )
{
a[i] = rand() % N;
}
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
bubble_sort( a, N );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
putchar( '\n' );
}
return 0;
}
In the program an array of random numbers is sorted 10 times.
The program output might look for example like.
4 0 1 0 5 7 1 1 5 2
0 0 1 1 1 2 4 5 5 7
8 1 1 0 7 1 3 1 1 0
0 0 1 1 1 1 1 3 7 8
6 0 8 2 8 3 7 4 7 8
0 2 3 4 6 7 7 8 8 8
2 1 0 3 4 5 3 7 8 0
0 0 1 2 3 3 4 5 7 8
9 6 3 0 9 0 4 4 4 5
0 0 3 4 4 4 5 6 9 9
5 2 7 5 4 7 0 1 2 7
0 1 2 2 4 5 5 7 7 7
1 4 1 4 9 5 1 4 4 0
0 1 1 1 4 4 4 4 5 9
6 5 8 0 7 9 2 1 4 6
0 1 2 4 5 6 6 7 8 9
9 1 9 6 6 5 4 8 9 8
1 4 5 6 6 8 8 9 9 9
5 2 4 6 6 5 3 0 2 7
0 2 2 3 4 5 5 6 6 7
If you are going to use the same sorting function to sort an array in ascending and descending orders then the function can look as it is shown in the demonstrative program below.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void bubble_sort( int a[], size_t n, int cmp( int, int ) )
{
for ( size_t last = n; !( n < 2 ); n = last )
{
last = 0;
for ( size_t i = 1; i < n; i++ )
{
if ( cmp( a[i], a[i-1] ) )
{
int tmp = a[i];
a[i] = a[i-1];
a[i-1] = tmp;
last = i;
}
}
}
}
int ascending( int x, int y )
{
return x < y;
}
int descending( int x, int y )
{
return y < x;
}
int main(void)
{
enum { N = 10 };
int a[N];
srand( ( unsigned int )time( NULL ) );
for ( size_t i = 0; i < N; i++ )
{
a[i] = rand() % N;
}
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
bubble_sort( a, N, ascending );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
bubble_sort( a, N, descending );
for ( size_t i = 0; i < N; i++ )
{
printf( "%d ", a[i] );
}
putchar( '\n' );
return 0;
}
The program output might look like
9 0 1 6 0 8 7 4 9 4
0 0 1 4 4 6 7 8 9 9
9 9 8 7 6 4 4 1 0 0
/******************************************************************************
Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <stdio.h>
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
int size=10, numbers[size], i, order;
for (i=0; i<10; i++)
{
printf("please enter a number:");
scanf("%d", &numbers[i]);
}
ascending(numbers, size);
return 0;
}
void ascending(int numbers[], int size)
{
int temp, i, sflag, count=0;
do
{
sflag = 0;
for(i=1; i <10; i++)
{
if (numbers[i-1] > numbers[i])
{
temp = numbers[i-1];
numbers[i-1] = numbers[i];
numbers[i] = temp;
sflag = 1;
}
}
count++;
}while(sflag);
for (i=0; i<10; i++)
{
printf("%d\t", numbers[i]);
}
}
Running your code slightly modified (make it compile able) in https://www.onlinegdb.com/online_c_compiler#
I was not able to detect any error
I checkt 3,7,8,8,9,10,11,200,317 and 1,1,1,1,1,1,1,1,1
There are 2 mistakes in your code:
on line 13 you are passing numbers[] to the ascending function. This is wrong, you can never pass anything with [], when you call a function. When you write int numbers[] in the argument list of a function, it means that you want the function to accept a pointer to a number, you are just declaring this to the compiler. So it should just be ascending(numbers, size);
on line 30 you made a typo, you wrote unmbers[i] = temp;, while it should be numbers[i] = temp;
here is the correct code:
#include <stdio.h>
#include <conio.h>
void ascending(int numbers[], int size);
int main()
{
int size=10, numbers[size], i, order;
for (i=0; i<10; i++)
{
printf("please enter a number:");
scanf("%d", &numbers[i]);
}
ascending(numbers, size);
}
void ascending(int numbers[], int size)
{
int temp, i, sflag, count=0;
do
{
sflag = 0;
for(i=1; i <10; i++)
{
if (numbers[i-1] > numbers[i])
{
temp = numbers[i-1];
numbers[i-1] = numbers[i];
numbers[i] = temp;
sflag = 1;
}
}
count++;
}while(sflag);
for (i=0; i<10; i++)
{
printf("%d\t", numbers[i]);
}
}
I am trying to print an array in order to the even strings print backwards but the not even string in usual way. What do I do wrong with it?
For example:
1 0 3
9 7 3
5 7 8
and I need it:
1 0 3
3 7 9
5 7 8
But I also have a problem with filling an array in spiral way; how should I take a center of an array? Please, could you give an idea — how should I do this? And the array must be square. For example:
1 2 3
4 5 6
7 8 9
but I need it:
3 2 9
4 1 8
5 6 7
My code so far:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a[10][10],n,m,i,j;
printf("Enter m: ");
scanf("%d",&m);
printf("Enter n: ");
scanf("%d",&n);
for(i=0;i<m;i++){
for(j=0;j<m;j++){
printf("a[%d][%d]: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
// in usual order
for(i=0;i<m;i++){
for(j=0;j<n;j++){
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;i++){
for(j=0;j<n;j++){
if(i%2 != 0){
printf("%d ",a[i][j]);
}
else {
printf("%d ",a[n-i+1][j]);
}
}
printf("\n");
}
return 0;
}
example of filling an array in spiral
#include <stdio.h>
#include <string.h>
typedef enum {
N, W, S, E
} Dir;
typedef struct walker {
int row, col;
Dir dir;
int steps;
} Walker;
Walker go_forward(Walker walker){
switch(walker.dir){
case N:
walker.row -= 1;
break;
case W:
walker.col -= 1;
break;
case S:
walker.row += 1;
break;
case E:
walker.col += 1;
break;
}
return walker;
}
Walker proceed_left(Walker walker){
walker.dir = (walker.dir + 1) % 4;//turn left
walker = go_forward(walker);
return walker;
}
int main(void){
int n;
for(;;){
printf("Enter n(0 < n < 10): ");fflush(stdout);
int ret_s = scanf("%d", &n);
if(ret_s == 1){
if(0 < n && n < 10)
break;
} else if(ret_s == 0)
while(getchar() != '\n');//clear input
else //if(ret_s == EOF)
return 0;
}
int a[n][n];
memset(a, 0, sizeof(a));//zero clear
Walker walker = { .row = n / 2, .col = n / 2, .dir = E, .steps = 0 };
for(;;){
walker.steps += 1;
a[walker.row][walker.col] = walker.steps;
if(walker.steps == n * n)//goal
break;
Walker left = proceed_left(walker);
if(a[left.row][left.col] == 0)//left side is vacant
walker = left;
else
walker = go_forward(walker);
}
for(int r = 0; r < n; ++r){
for(int c = 0; c < n; ++c){
if(c)
putchar(' ');
printf("%2d", a[r][c]);
}
puts("");
}
}
Here is a program that includes the function spiral_fill(), which fills a square array with sequential ints, starting from 1 at the center, and proceeding in a counter-clockwise spiral. The function fills the array by first storing a 1 in the center, then filling the L-shaped region above and to the left, then below and to the right, and continuing until the array is filled.
#include <stdio.h>
#define ARR_SZ 3
void spiral_fill(size_t arr_sz, int arr[arr_sz][arr_sz]);
void print_arr(size_t rows, size_t cols, int arr[rows][cols]);
int main(void)
{
int test_arr[ARR_SZ][ARR_SZ];
spiral_fill(ARR_SZ, test_arr);
print_arr(ARR_SZ, ARR_SZ, test_arr);
return 0;
}
void spiral_fill(size_t arr_sz, int arr[arr_sz][arr_sz])
{
int center = arr_sz / 2;
int current = center;
int start_col, stop_col, start_row, stop_row;
size_t layer = 0;
int next_val = 1;
arr[center][center] = next_val++;
++layer;
while (layer < arr_sz) {
if (layer % 2) { // For odd layers, fill upper L
current -= layer;
start_col = center + layer / 2;
stop_col = center - (layer + 1) / 2;
for (int j = start_col; j >= stop_col; j--) {
arr[current][j] = next_val++;
}
start_row = center - layer / 2;
stop_row = center + layer / 2;
for (int i = start_row; i <= stop_row; i++) {
arr[i][current] = next_val++;
}
++layer;
} else { // For even layers, fill lower L
current += layer;
start_col = center - layer / 2;
stop_col = center + layer / 2;
for (int j = start_col; j <= stop_col; j++) {
arr[current][j] = next_val++;
}
start_row = center + (layer - 1) / 2;
stop_row = center - layer / 2;
for (int i = start_row; i >= stop_row ; i--) {
arr[i][current] = next_val++;
}
++layer;
}
}
}
void print_arr(size_t rows, size_t cols, int arr[rows][cols])
{
for (size_t i = 0; i < rows; i++) {
for (size_t j = 0; j < cols; j++) {
printf("%-5d ", arr[i][j]);
}
putchar('\n');
}
}
Here is a 3X3 array:
3 2 9
4 1 8
5 6 7
Here is a 6X6 array:
31 30 29 28 27 26
32 13 12 11 10 25
33 14 3 2 9 24
34 15 4 1 8 23
35 16 5 6 7 22
36 17 18 19 20 21
How do I print this?
1 3 6 10 15
2 5 9 14
4 8 13
7 12
11
I am able to come out the following code but I am unsure on how I can combine the following to produce the result I want.
#include <stdio.h>
int main()
{
int n = 5;
int start = a;
int i;
int j;
for (j = 1; j <= n; j++) /* This for loop produce 1 3 6 10 15 */
{
start = start + j;
printf("%d ", start);
}
printf("\n");
start = 1;
for (i = 0; i < 5; i++) /* This for loop produce 1 2 4 7 11 */
{
start = start + i;
printf("%d ", start);
}
return 0;
}
Please guide me as I am really not good in programming.
If you just want to print the mentioned pattern the fastest way is:
#include <stdio.h>
int main(){
puts("1 3 6 10 15");
puts("2 5 9 14");
puts("4 8 13");
puts("7 12");
puts("11");
return 0;
}
P.S: You are not taking any input from user.
Use nested loops.
#include <stdio.h>
int main()
{
int n = 5;
int row_start = 1, step, start_step = 2; row_step = 1, current;
for (int j = 1; j <= n; j++) /* This for loop produces the rows */
{
current = step = row_start;
step = start_step;
for(int i = j; i <= 5; i++) /* This for loop produces one row */
{
printf("%d ", current);
current += step;
++step;
}
printf("\n");
row_start += row_step;
++row_step;
++start_step;
}
return 0;
}
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
int mas[200][200],i = 0,j = 0;
char c[20];
int x,y,max;
// Function to read array from txt file.//
void read()
{
FILE *fp = fopen("masyvas.txt","r");
while(!feof (fp))
{
fscanf (fp, "%d", &mas[i][j]);
i++;
printf("%d\n",i);
fscanf(fp,"%c",&c);
printf("%c\n",c);
if(*c == '\n')
{
j++;
i = 0;
}
}
i = 0;
j = 0;
fclose(fp);
}
// Function to find min and max from array need to be improved to find min in row and max in column of same element.//
void find()
{
int a = 0,max = 0,min = 200,b = 0,sk = 45,g = 0;
while(sk != 0)
{
if(mas[a][b] <= min)
{
min = mas[a][b];
}
if(mas[a][b] > max)
{
max = mas[a][b]
}
a++;
if(a == 9)
{
b++;
a = 0;
{
sk--;
}
}
Text File:
5 4 6 4 6 5 4 6 5
7 3 4 5 4 3 5 2 2
6 2 3 6 4 6 4 5 7
3 1 4 5 3 4 6 3 4
2 3 5 2 3 5 2 6 7
Guys I am out of options I tried almost everything I can't think out the way Look at void find() Tell me what must be changed and how I want to find Element which is min ir row and max in his column.
Can find min and max now I made a == 9 becouse it represents position of column in array then it comes to the end of line(9) position in line goes back to 0 and b count of rows is added. I need to find which array elements has minimum value in row and maximum in column. I hope I explained it well.
This is a naive solution but it does what you need.
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdlib.h>
#include<stdio.h>
#include<ctype.h>
int matrix[200][200];
void readMatrix(int *rows, int *cols)
{
int rowIdx = 0;
int colIdx = 0;
char c;
FILE *file = fopen("input.txt", "r");
while (!feof(file))
{
fscanf(file, "%d", &matrix[rowIdx][colIdx]);
colIdx++;
fscanf(file, "%c", &c);
if (c == '\r' || c == '\n')
{
++rowIdx;
*cols = colIdx;
colIdx = 0;
}
}
*rows = rowIdx;
fclose(file);
}
int isMinOnRow(int rowIdx, int colIdx, int cols)
{
int value = matrix[rowIdx][colIdx];
int cI;
for (cI = 0; cI < cols; ++cI)
{
if (matrix[rowIdx][cI] < value)
return 0;
}
return 1;
}
int isMaxOnCol(int rowIdx, int colIdx, int rows)
{
int value = matrix[rowIdx][colIdx];
int rI;
for (rI = 0; rI < rows; ++rI)
{
if (matrix[rI][colIdx] > value)
return 0;
}
return 1;
}
void findMinMax(int rows, int cols, int *rowIdx, int *colIdx)
{
int bFound = 0;
int rI, cI;
for (rI = 0; rI < rows; ++rI)
{
for (cI = 0; cI < cols; ++cI)
{
if (isMinOnRow(rI, cI, cols) && isMaxOnCol(rI, cI, rows))
{
*rowIdx = rI;
*colIdx = cI;
bFound = 1;
break;
}
}
if (bFound)
break;
}
}
int main()
{
int cols, rows;
int rowIdx, colIdx;
readMatrix(&rows, &cols);
findMinMax(rows, cols, &rowIdx, &colIdx);
printf("max min at [%d, %d] = %d\n", rowIdx, colIdx, matrix[rowIdx][colIdx]);
return 0;
}
Basically what it does is to iterate the matrix and check for each element if it meets required conditions.
a++;
if(a == 9)
b++;
a = 0;
This should be:
a++;
if(a == 9) {
b++;
a = 0;
}
Also it should probably be (a == 200) since that's the size of the array.
EDIT: Wait, I see now the values of 9 and 45 refer to there being 5 rows of 9 values.