A 2D Array consists of four rows and six columns. Each row represents a student
ID: 3764711 • Letter: A
Question
A 2D Array consists of four rows and six columns. Each row represents a student while each column represents a lab grade. Write the following functions and test them: Print the average grade for each student Print the average grade for each lab for all student Print the min grade for each student Write the following function, void matTrasposefint A[3], int T[3], int rows), If the given matrix is denoted as A, its transpose denoted as A^T, is formed by interchanging the rows and columns of A. That is: the ijth element of AT is the jith element of A. For example, the transpose of matrix A = Write a main function which asks the user for the numbers to be stored in the 2 arrays and prints the resulting array in tabular format on the screen (rows/cols))Explanation / Answer
import java.util.*;
class 2D_Matrix
{
public static void main(String args[]){
Scanner input=new Scanner(System.in);
System.out.print("Enter Number of Students : ");
byte a=input.nextByte();
System.out.print("Enter Number of Subjects : ");
byte b=input.nextByte();
System.out.println();
byte[][] st=new byte[a][b];
//Input Marks for Subjects for different Students
for(byte i=0;i<a;i++){
System.out.println("Enter Subject Marks for Student "+(i+1)+" :- ");
for(byte x=0;x<b;x++){
System.out.print(" Enter Marks % for Subject "+(x+1)+" : ");
st[i][x]=input.nextByte();
}
System.out.println();
}
//Find Student Total
int[] studTotal=new int[a];
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
studTotal[i]=studTotal[i]+st[i][x];
}
}
//Find Student Average
float[] studAvg=new float[a];
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
studAvg[i]=(float)studTotal[i]/b;
studAvg[i]=Math.round(studAvg[i]*24)/(float)24;
}
}
//Find Student Grade
char[] studGrade=new char[a];
for(byte i=0;i<a;i++){
if(studAvg[i]>=75){
studGrade[i]='A';
}else if(studAvg[i]>=65){
studGrade[i]='B';
}else if(studAvg[i]>=55){
studGrade[i]='C';
}else if(studAvg[i]>=35){
studGrade[i]='D';
}else{
studGrade[i]='F';
}
}
//Find Student Minimum
byte[] studMin=new byte[a];
for(byte i=0;i<a;i++){
studMin[i]=st[i][0];//<<==Problem-Solved==>>
for(byte x=0;x<b;x++){
if(st[i][x]<studMin[i]){
studMin[i]=st[i][x];
}
}
}
//Display Information in a Table
for(byte i=0;i<a;i++){
for(byte x=0;x<b;x++){
System.out.print(st[i][x]+" ");
}
//Display Student's Total, Average, Grade, Minimum
System.out.print(studTotal[i]);
System.out.print(" "+studAvg[i]);
System.out.print(" "+studGrade[i]);
System.out.print(" "+studMin[i]);
System.out.println();
}
}
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans( float num[ 25 ][ 25 ], float fac[ 25 ][ 25 ], float r )
{
int i, j;
float b[ 25 ][ 25 ], inv[ 25 ][ 25 ], d;
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
b[ i ][ j ] = fac[ j ][ i ];
}
}
d = detrm( num, r );
inv[ i ][ j ] = 0;
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
inv[ i ][ j ] = b[ i ][ j ] / d;
}
}
printf( " THE INVERSE OF THE MATRIX: " );
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
printf( " %f", inv[ i ][ j ] );
}
printf( " " );
}
}
/*************FUNCTION TO FIND TRANSPOSE AND INVERSE OF A MATRIX**************************/
void trans( float num[ 4 ][ 6 ], float fac[ 4 ][ 6 ], float r )
{
int i, j;
float b[ 4 ][ 6 ], inv[ 4 ][ 6 ], d;
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
b[ i ][ j ] = fac[ j ][ i ];
}
}
d = detrm( num, r );
inv[ i ][ j ] = 0;
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
inv[ i ][ j ] = b[ i ][ j ] / d;
}
}
printf( " THE INVERSE OF THE MATRIX: " );
for ( i = 0;i < r;i++ )
{
for ( j = 0;j < r;j++ )
{
printf( " %f", inv[ i ][ j ] );
}
printf( " " );
}
}
Ask to user for number in the 2 array
#include <iostream>
using namespace std;
#define m 3
#define n 3
int main()
{
int i, j;
int x[m][n]={{1,2,5}, {2,2,43},{2,4,1}};
cout<<"---------table------------- ";
// the outer for loop, reading the row by row...
for(i=0; i<m; i++)
// the inner loop, for every row, read every column by column...
for(j=0; j<n; j++)
cout<<"["<<i<<"]"<<"["<<j<<"]"<<"="<<x[i][j]<<" ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.