requiring a C monkeh

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Code:
#include <stdio.h>
#define ROWS    6  //define number of rows; change here to change everywhere
#define COLUMNS 6  //define number of columns; change here to change everywhere

void menu();

#define Display_distance();
#define Calc_cost();
#define Mileage_chart();
#define Exit();

void Mileage_chart(int Array2D[][]); //declaration of a function

int main() {

    int i,j;
    int Array2D[ROWS][COLUMNS];

    for(i=0; i<ROWS; i++) {
      printf("Please Enter 6 Distances. Include a space between each: \n");
      for(j=0; j <COLUMNS; j++) {
        scanf_s("%d" , &Array2D[i][j]);
      }
      printf("\n");
    }

    printf("The Distances you entered are: \n");
    for(i=0; i<ROWS; i++) {
      for(j=0; j<COLUMNS; j++) {
        printf("%d ", Array2D[i][j]);
      }
	
      printf("\n");
    }
     
    menu();
	menu(Array2D);

    return 0;  
}

void menu(void) {

int input;

	printf("\n");
    printf( "1. Display Distance Between Two Points\n" );
    printf( "2. Calcuate Cost of a Journey at 40p per mile\n" );
    printf( "3. Display On Screen the full Mileage Chart\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf_s( "%d", &input );
    switch ( input ) {
        case 1:
            Display_distance();
            break;
        case 2:          
            Calc_cost();
            break;
        case 3:        
            Mileage_chart();
			Mileage_chart(Array2D);
            break;
		case 4:
			Exit();
			break;
        default:            
            printf( "Bad input, Exiting Program\n" );
            break;
    }
    getchar();

}

Im trying to get the printed output of the 2d array to be displayed through case#3. Can anyone see where im going wrong?:/
 

Lakih

Resident Freddy
Joined
Dec 23, 2003
Messages
1,637
It was quite a few years ago i did anything in C. As far as i can see (and i can be horribly wrong) the function "Mileage_chart" is declared but never acutally used. And why the multiple #define at the top (Display_distance, Calc_cost, Mileage_chart, Exit) ?

How does this printing looks?
printf("The Distances you entered are: \n");
for(i=0; i<ROWS; i++) {
for(j=0; j<COLUMNS; j++) {
printf("%d ", Array2D[j]);
}
Cant you use that in the "mileage_chart" fuction?


I dont have a C editor installed sady so i cant help more then that... if it was any help at all :p
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Code:
#include <stdio.h>
#define ROWS    6  //define number of rows; change here to change everywhere
#define COLUMNS 6  //define number of columns; change here to change everywhere

void menu(int Array2D[ROWS][COLUMNS]);

void Mileage_chart(int Array2D[ROWS][COLUMNS]); //declaration of a function

int main() {

    int i,j;
    int Array2D[ROWS][COLUMNS];

//for loop to allow user to input data into an array

    for(i=0; i<ROWS; i++) {
      printf("Please Enter 6 Distances. Include a space between each: \n");
      for(j=0; j <COLUMNS; j++) {
        scanf("%d" , &Array2D[i][j]);
      }
      printf("\n");
    }

	printf("\n");

//printing the array 

    printf("The Distances you entered are: \n");
    for(i=0; i<ROWS; i++) {
      for(j=0; j<COLUMNS; j++) {
        printf("%d ", Array2D[i][j]);
      }
      printf("\n");
    }    
    menu(Array2D);

    return 0;  
}

//case statement for the Menu after the array is printed

void menu(int Array2D[ROWS][COLUMNS]) {

int input;

    printf("\n");
    printf( "1. Display Distance Between Two Points\n" );
    printf( "2. Calcuate Cost of a Journey at 40p per mile\n" );
    printf( "3. Display On Screen the full Mileage Chart\n" );
    printf( "4. Exit\n" );
    printf( "Selection: " );
    scanf( "%d", &input );
    switch ( input ) {
        case 1:
            //Display_distance();
            break;
        case 2:          
            //Calc_cost();
            break;
        case 3:            
            Mileage_chart(Array2D);
            break;
        case 4:
            //Exit();
            break;
        default:            
            printf( "Bad input, Exiting Program\n" );
            break;
    }
    getchar();

}
//function for milage chart

void Mileage_chart(int Array2D[ROWS][COLUMNS]){
    int i,j;
    printf("The Full Mileage Chart: \n");
    
    for(i=0; i<ROWS; i++) {
      for(j=0; j<COLUMNS; j++) {
        printf("%d", Array2D[i][j]);
      }
    }
}

got it working, although it doesnt print an array the second time round, it does it on a straight line! :p

73055683np3.jpg
 

Lakih

Resident Freddy
Joined
Dec 23, 2003
Messages
1,637
Code:
printf("The Distances you entered are: \n");
    for(i=0; i<ROWS; i++) {
      for(j=0; j<COLUMNS; j++) {
        printf("%d ", Array2D[i][j]);
      }
     printf("\n");
    }

You forgot the last "printf("/n");" in the function.

Compare the code for the first and second printing of the data
 

- English -

Resident Freddy
Joined
Apr 7, 2004
Messages
5,263
Code:
printf("The Distances you entered are: \n");
    for(i=0; i<ROWS; i++) {
      for(j=0; j<COLUMNS; j++) {
        printf("%d ", Array2D[i][j]);
      }
     printf("\n");
    }

You forgot the last "printf("/n");" in the function.

Compare the code for the first and second printing of the data

ahha thanks,

would you know how to write a function to calculate 1 number in the array against another?:p
 

Lakih

Resident Freddy
Joined
Dec 23, 2003
Messages
1,637
ahha thanks,

would you know how to write a function to calculate 1 number in the array against another?:p

You can use standard arithmetic operators on a two-dimensional array.

Sum = Myarray [0][0] + Myarray [0][1] would make sum 5 if the data would be. 2, 3 and 1, 2 (rows, columns).

(im to tierd to think 2d and write C atm :( )
 

Ovron

One of Freddy's beloved
Joined
Nov 4, 2004
Messages
471
You can use standard arithmetic operators on a two-dimensional array.

Sum = Myarray [0][0] + Myarray [0][1] would make sum 5 if the data would be. 2, 3 and 1, 2 (rows, columns).

(im to tierd to think 2d and write C atm :( )

You can /not/ do arithmetic operations on arrays. The example you gave above is an arithmetic operation on one-dimensional elements in the array.

You need to create algorithms for vector and matrix operations, or you can for instance use MATLAB (and its C-extension). We use it rather a lot in chemical engineering.
 

Lakih

Resident Freddy
Joined
Dec 23, 2003
Messages
1,637
You can /not/ do arithmetic operations on arrays. The example you gave above is an arithmetic operation on one-dimensional elements in the array.

You need to create algorithms for vector and matrix operations, or you can for instance use MATLAB (and its C-extension). We use it rather a lot in chemical engineering.

True, i was abit unclear. You cant do it on the array itself, but you can still do arithmetic operations on the elements in the array, doesnt matter if its one- or twodimensional.

Source? Programming in ANSI C by Ram Kumar & Rakesh Agrawal Chapter 6 p196
 

Users who are viewing this thread

Top Bottom