Q.Write a program using 2D array. A company manufactures three
products P1, P2 and P3. Company has hired five salesman. Each
salesman sell all three products. Accept from user number of each
product sold by each salesman. Find the product whose sale was
largest.Also find the salesman whose sales were the least.
-------------------------------------------------------------------------------------------------------
THIS IS THE QUESTION.I TRIED A LOT AND CAME UP WITH THIS SOLUTION BUT STILL GETTING ERRORS....PLZ HELP
import java.util.*;
class Salesman
{
public static void main(String args[])
{
int i,j;
int s[][]=new int[5][4];
int salesman_hs=s[0][3];
int salesman_ind=0;
int max_prod=0;
int prod_ind=0;
int sum;
Scanner sc= new Scanner(System.in);
for(i=0;i<=4;i++)
{
System.out.println("Enter product sold by salesman"+(i+1));
for(j=0;j<=2;j++)
{
System.out.println("Enter sale for product"+(j+1));
s[i][j]=sc.nextInt();
s[i][3]=s[i][3]+s[i][j];
}
if(s[i][3]>salesman_hs)
{
salesman_hs=s[i][3];
salesman_ind=i;
}
}
for(i=0;i<=2;i++)
{
sum=0;
for(j=0;j<=4;j++)
{
sum=sum+s[j][i];
}
if(max_prod<sum)
{
max_prod=sum;
prod_ind=i;
}
}
}
System.out.println("p1\t p2\t p3\t total");
for(i=0;i<=4;i++)
{
for(j=0;j<=3;j++)
{
System.out.print(s[i][j]+"\t");
}
System.out.println();
}
System.out.println("\n Salesman"+(salesman_ind+1)+"has highest sales of"+salesman_hs);
System.out.println("Product"+(prod_ind+1)+"has the highest sales of"+max_prod);
}
sumod
0
Newbie Poster
Recommended Answers
Jump to PostI'd read https://www.daniweb.com/programming/web-development/threads/435023/read-this-before-posting-a-question before you reply.
You accidentally put your entire post in a code block so that's OK. You can try harder from now on.
You wrote there are errors but didn't call out what the error was or what line it happens on. Add that next.
Jump to PostThis is a variable scope error. You declared the variables salesman_ind, prod_ind, salesman_hs, max_prod in the code bracketed by the brackets starting on lines 10 and ending on line 40. That means you cannot use those variables outside lines 10 to 40. They go "out of scope" after line 40 …
Jump to PostYou have a for-loop using i as the loop control variable in line 14. Then you have it again on line 23 and 31, so your inner loop counts are messing up your outer loop counts and vice-versa. Perhaps you want to use j instead of i as the loop …
All 11 Replies
rproffitt
2,565
"Nothing to see here."
Moderator
sumod
0
Newbie Poster
AssertNull
1,094
Practically a Posting Shark
sumod
0
Newbie Poster
sumod
0
Newbie Poster
AssertNull
1,094
Practically a Posting Shark
sumod
0
Newbie Poster
AssertNull
1,094
Practically a Posting Shark
sumod
0
Newbie Poster
sumod
0
Newbie Poster
sumod
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.