12 Replies - 1332 Views - Last Post: 27 February 2008 - 08:54 AM Rate Topic: -----

#1 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

function for adding elemets of an array

Posted 27 February 2008 - 04:51 AM

Hi,

code :

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


This is the loop that allows that prints out a array entered in by the user.

Now say I wanted to ask the user to pick an array slot and times whatever has been entered in this slot; for example in array [0][1] and * this by an integar, lets say 20.

I need to write a function to do this, would anyone set me off in the right direction?

thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: function for adding elemets of an array

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 04:59 AM

//code for selecting the array element using scanf();
//Storing the input in any ints x and y
//code for product
int input;
printf("Enter the number to multiply: ");
scanf("%d",&input);
printf("The product of the array element and your input is %d", array[x][y]*input);

//OR
int result;
result = array[x][y]*input;
printf("The product of the array element and your input is %d", result);



If you need a function..

It can look something like this
int product(int[][] Array2D){
//You can modify and place the above code here
}


This post has been edited by letthecolorsrumble: 27 February 2008 - 05:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 05:37 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 04:59 AM, said:

//code for selecting the array element using scanf();
//Storing the input in any ints x and y
//code for product
int input;
printf("Enter the number to multiply: ");
scanf("%d",&input);
printf("The product of the array element and your input is %d", array[x][y]*input);

//OR
int result;
result = array[x][y]*input;
printf("The product of the array element and your input is %d", result);



If you need a function..

It can look something like this
int product(int[][] Array2D){
//You can modify and place the above code here
}



cool, ive got the function to work. Ive kinda mastered them now, mainly due to your help :P

however, could you direct me in the way i need to go for this next question.

//function to take array elements and * by user inputted amount

void Calc_cost(int Array2D [ROWS][COLUMNS]){
int input_cost;  
   printf("Enter the amount in Pence the distance per mile: ");  
   scanf("%d",&input_cost);  
   printf("The product of the array element and your input is %d", Array2D[ROWS][COLUMNS]*input_cost); 

}



It doesnt ask the user what part of the array they would like to multipy. It just prints a random number out, from memory i think :P

if you need my full code please ask and ill post it.
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:18 AM

void Calc_cost(int Array2D [ROWS][COLUMNS]){
int input_cost;  
   printf("Enter the amount in Pence the distance per mile: ");  
   scanf("%d",&input_cost);  
   printf("The product of the array element and your input is %d", Array2D[ROWS][COLUMNS]*input_cost);
//ROWS and COLUMNS are CONSTANTS, so you can not use them to point to an element in an array
//The max index is always one less than the number of rows and columns in the array as the index
//starts from zero. You can but then you won't be inside the array anymore.
}



Just as you ask the user to input the cost, you can ask the user to input the row and column number and store them in other variables and replace ROWS and COLUMNS with those variable names in the printf.
Was This Post Helpful? 0
  • +
  • -

#5 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:27 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 06:18 AM, said:

void Calc_cost(int Array2D [ROWS][COLUMNS]){
int input_cost;  
   printf("Enter the amount in Pence the distance per mile: ");  
   scanf("%d",&input_cost);  
   printf("The product of the array element and your input is %d", Array2D[ROWS][COLUMNS]*input_cost);
//ROWS and COLUMNS are CONSTANTS, so you can not use them to point to an element in an array
//The max index is always one less than the number of rows and columns in the array as the index
//starts from zero. You can but then you won't be inside the array anymore.
}



Just as you ask the user to input the cost, you can ask the user to input the row and column number and store them in other variables and replace ROWS and COLUMNS with those variable names in the printf.


so theres noway a user can display the distance between two valid points, for example point 1 and 6?
Was This Post Helpful? 0
  • +
  • -

#6 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:37 AM

You can do that, then you just need to code a bit more ! ;)
Was This Post Helpful? 0
  • +
  • -

#7 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:42 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 06:37 AM, said:

You can do that, then you just need to code a bit more ! ;)


know any tutorials i can look at to get started?

notsure what im meant to be looking for
Was This Post Helpful? 0
  • +
  • -

#8 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:48 AM

Could you explain in detail, what task you have at hand. I could then guide you better.
Was This Post Helpful? 0
  • +
  • -

#9 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 06:53 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 06:48 AM, said:

Could you explain in detail, what task you have at hand. I could then guide you better.


Sure

#include <stdio.h>
#define ROWS	3  //define number of rows; change here to change everywhere
#define COLUMNS 3  //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
void Calc_cost(int Array2D[ROWS][COLUMNS]);
void Display_distance(int Array2D[ROWS][COLUMNS]);

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 3 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(Array2D);
			break;
		case 3:			
			Mileage_chart(Array2D);
			break;
		case 4:
			//Exit();
			break;
		default:			
			printf( "Bad input, Exiting Program\n" );
			break;
	}

	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 );
}
//function for milage chart

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

}

//function to take array elements and * by user inputted amount

void Calc_cost(int Array2D [ROWS][COLUMNS]){
int input_cost = 40; 
int input_distance;
   printf("\n");
   printf("Enter distance Travelling\n");
   scanf("%d",&input_distance);
   printf("The product of distance and price per mile at 40pence is: %d\n", input_distance * input_cost); 
   printf("\n");

}

//function to display distance between two points of the array

void Display_distance() {
	int distance;
	int point1;
	int point2;
	printf("\n");
	printf("Enter the points you wish to see the distance between:\n");
	scanf("%d",&point1);
	scanf("%d",&point2);
	printf("the points you entered are: ", &point1, &point2);

}



This is my code, I had the last function working a min ago, just apparently not enough argunments now or w/e that means.

Im trying to display the distance between two array points so for example
[1][10][30]
[2][3][2]
[4][2][2]

so the difference between point [0][0] and [1][1] is 2.. ya? (3-1 = 2)

When my program prints the array, the user should be able to look at input two numbers, then the program should basically biggest - smallest which gives the difference.

That understandable?

This post has been edited by fremen: 27 February 2008 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#10 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 07:34 AM

Yeah gotcha!


#include <stdio.h>
#define ROWS    3  //define number of rows; change here to change everywhere
#define COLUMNS 3  //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
void Calc_cost(int Array2D[ROWS][COLUMNS]);
void Display_distance(int Array2D[ROWS][COLUMNS]);//the signature at declartion...

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 3 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;
	do{
		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(Array2D); //need to pass parameter, here the array
				//should match the signature at the call of a function.
				break;
			case 2:          
				Calc_cost(Array2D);
				break;
			case 3:            
				Mileage_chart(Array2D);
				break;
			case 4:
				break;
			default:            
				printf( "Bad input, Exiting Program\n" );
				break;
		}
	}while(input!=4); //loop to remain in menu
}
//function for milage chart

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

//function to take array elements and * by user inputted amount

void Calc_cost(int Array2D [ROWS][COLUMNS]){ 
	int input_cost = 40;
	int input_distance;
	printf("\n");
	printf("Enter distance Travelling\n");
	scanf("%d",&input_distance);
	printf("The product of distance and price per mile at 40pence is: %d\n", input_distance * input_cost);
	printf("\n");

}

//function to display distance between two points of the array

void Display_distance(int Array2D[ROWS][COLUMNS]) {//should match signature at defination.. 
    int distance;
    int row1,column1, row2,column2;
    printf("\n");
    printf("Enter the points you wish to see the distance between:\n");
	printf("Enter Row and Column of first point (1-3)");
    scanf("%d",&row1);
    scanf("%d",&column1);
	printf("Enter Row and Column of second point (1-3)");
    scanf("%d",&row2);
    scanf("%d",&column2);
	
	distance = (Array2D[row1-1][column1-1])-(Array2D[row2-1][column2-1]);

	if(distance<0){
		distance = -distance;
	}

	printf("The distance between two points you entered is:%d \n", distance);
	printf("\n");

}


This post has been edited by letthecolorsrumble: 27 February 2008 - 07:35 AM

Was This Post Helpful? 0
  • +
  • -

#11 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 08:03 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 07:34 AM, said:

Yeah gotcha!


#include <stdio.h>
#define ROWS    3  //define number of rows; change here to change everywhere
#define COLUMNS 3  //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
void Calc_cost(int Array2D[ROWS][COLUMNS]);
void Display_distance(int Array2D[ROWS][COLUMNS]);//the signature at declartion...

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 3 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;
	do{
		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(Array2D); //need to pass parameter, here the array
				//should match the signature at the call of a function.
				break;
			case 2:          
				Calc_cost(Array2D);
				break;
			case 3:            
				Mileage_chart(Array2D);
				break;
			case 4:
				break;
			default:            
				printf( "Bad input, Exiting Program\n" );
				break;
		}
	}while(input!=4); //loop to remain in menu
}
//function for milage chart

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

//function to take array elements and * by user inputted amount

void Calc_cost(int Array2D [ROWS][COLUMNS]){ 
	int input_cost = 40;
	int input_distance;
	printf("\n");
	printf("Enter distance Travelling\n");
	scanf("%d",&input_distance);
	printf("The product of distance and price per mile at 40pence is: %d\n", input_distance * input_cost);
	printf("\n");

}

//function to display distance between two points of the array

void Display_distance(int Array2D[ROWS][COLUMNS]) {//should match signature at defination.. 
    int distance;
    int row1,column1, row2,column2;
    printf("\n");
    printf("Enter the points you wish to see the distance between:\n");
	printf("Enter Row and Column of first point (1-3)");
    scanf("%d",&row1);
    scanf("%d",&column1);
	printf("Enter Row and Column of second point (1-3)");
    scanf("%d",&row2);
    scanf("%d",&column2);
	
	distance = (Array2D[row1-1][column1-1])-(Array2D[row2-1][column2-1]);

	if(distance<0){
		distance = -distance;
	}

	printf("The distance between two points you entered is:%d \n", distance);
	printf("\n");

}



#include <stdio.h>
#define ROWS	3  //define number of rows; change here to change everywhere
#define COLUMNS 3  //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
void Calc_cost(int Array2D[ROWS][COLUMNS]);
void Display_distance(Array2D);

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

	do{

	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(Array2D);
			break;
		case 2:		  
			Calc_cost(Array2D);
			break;
		case 3:			
			Mileage_chart(Array2D);
			break;
		case 4:
			break;
		default:			
			printf( "Bad input, Exiting Program\n" );
			break;
	}

	}while(input!=4);
}

	//function for milage chart

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

}

//function to take array elements and * by user inputted amount

void Calc_cost(int Array2D [ROWS][COLUMNS]){
int input_cost = 40; 
int input_distance;
   printf("\n");
   printf("Enter distance Travelling\n");
   scanf("%d",&input_distance);
   printf("The product of distance and price per mile at 40pence is: %d\n", input_distance * input_cost); 
   printf("\n");

}

//function to display distance between two points of the array

void Display_distance(Array2D) {
	int distance;
	int row1,column1, row2,column2;
	
	printf("\n");  
	printf("Enter the points you wish to see the distance between:\n");  
	printf("Enter Row and Column of first point (1-3)");  
	scanf("%d",&row1);  
	scanf("%d",&column1);  
	printf("Enter Row and Column of second point (1-3)");  
	scanf("%d",&row2);  
	scanf("%d",&column2); 
 
	distance = (Array2D[row1-1][column1-1])-(Array2D[row2-1][column2-1]);  

		if(distance<0){  
		distance = -distance;  
	}  
	printf("The distance between two points you entered is:%d \n", distance);  
	printf("\n");  

}  


1>.\assisgment.c(121) : error C2109: subscript requires array or pointer type
1>.\assisgment.c(121) : error C2109: subscript requires array or pointer type

hmmm :< Any idea over this error?
Was This Post Helpful? 0
  • +
  • -

#12 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 08:08 AM

change
void Display_distance(Array2D) {
to
void Display_distance(int Array2D[ROWS][COLUMNS]) {

It is in my code..you could have checked it yourself ! :)
Was This Post Helpful? 0
  • +
  • -

#13 fremen  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 08-November 07

Re: function for adding elemets of an array

Posted 27 February 2008 - 08:54 AM

View Postletthecolorsrumble, on 27 Feb, 2008 - 08:08 AM, said:

change
void Display_distance(Array2D) {
to
void Display_distance(int Array2D[ROWS][COLUMNS]) {

It is in my code..you could have checked it yourself ! :)



<3 thanks so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1