2 Replies - 327 Views - Last Post: 21 July 2011 - 08:51 PM Rate Topic: -----

#1 bratty_programmer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-June 11

Multiplying Arrays

Posted 21 July 2011 - 08:22 PM

I am given two arrays with numbers and i have to multiply them both to get the the third array which is the answer. My code is working but for some reason it's not multiplying the numbers correctly. The programme tends to give me the wrong answers for the third array. Maybe someone can take a look at it and guide me on what i am doing wrong please.

View Postbratty_programmer, on 22 July 2011 - 03:19 AM, said:

I am given two arrays with numbers and i have to multiply them both to get the the third array which is the answer. My code is working but for some reason it's not multiplying the numbers correctly. The programme tends to give me the wrong answers for the third array. Maybe someone can take a look at it and guide me on what i am doing wrong please. The code is at the bottom

#include <stdio.h>
main(){
int array1[6]={4,20,55,24,60,10};
int array2[6]={2,30,10,12,20,33};
int array3[6];
	printf("\n\nArray one(1):\t");
	for (int i=0;i<6;i++){
	printf("%d\t",array1[i]);
	}
	printf("\n\nArray two(2):\t");
	for (int j=0;j<6;j++){
	printf("%d\t",array2[j]);
	}
	printf("\n\n------------------------------------------------------------");
   printf("\nResult of multiplying Array1 by Array2:");
	printf("\n\nArray three(3):\t");
	//for (i=0;i<6;i++);
	//for (j=0;j<6;j++);
	for(int k=0;k<6;k++){
	array3[k]=(array1[i]*array2[j]);
	printf("%d\t",array3[k]);
	}
	}


This post has been edited by macosxnerd101: 21 July 2011 - 08:23 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Multiplying Arrays

#2 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Multiplying Arrays

Posted 21 July 2011 - 08:32 PM

I think all the arrays need to have k as the index in the product statement.
e.g.
for(int k=0;k<6;k++)
array3[k]=array1[k]*array2[k];
printf("%d\t",array3[k]);

Was This Post Helpful? 0
  • +
  • -

#3 bratty_programmer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 23-June 11

Re: Multiplying Arrays

Posted 21 July 2011 - 08:51 PM

thanks for you tip it worked
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1