0% found this document useful (0 votes)
9 views17 pages

Java Array Operations and Analysis

Uploaded by

kandanshetty29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views17 pages

Java Array Operations and Analysis

Uploaded by

kandanshetty29
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

import [Link].

*;
class Arrays_P1
{
static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("Enter n");
int n=[Link]();/**stores the size of array*/
int a[]=new int[n];/**stores n integers*/
[Link]("Enter "+n+" integers");
for(int i=0;i<[Link];i++)/**i is used as index number*/
a[i]=[Link]();
int count=0;/**count of numbers divisible 7 or ends with 7*/
int p=1;/**product of even integers*/
[Link]("Negtive numbers list....");
for(int i=0;i<[Link];i++)
{
if(a[i]<0)
[Link](a[i]);
if(a[i]%7==0 || a[i]%10==7)
count++;
if(a[i]%2==0)
p=p*a[i];
}
[Link]("Count of numbers divisible 7 or ends with 7 ="+count);
[Link]("Product of even integers="+p);
}
}
import [Link].*;
class Arrays_P2
{
void main()
{
Scanner sc=new Scanner ([Link]);
int a[]=new int[10];/**stores 10 integers*/
[Link]("Enter 10 integers");
for(int i=0;i<[Link];i++)/**i is used as index number*/
a[i]=[Link]();
[Link]("Perfect Squares...");
for(int i=0;i<[Link];i++)
{
int sqrt=(int)[Link](a[i]);
/**finding square root of a number*/
if(sqrt*sqrt==a[i])
[Link](a[i]);
}
}
}
import [Link].*;
class Arrays_P3
{
static void main()
{
Scanner sc=new Scanner ([Link]);
[Link]("Enter n");
int n=[Link]();/**stores the size of array*/
int marks[]=new int[n];/**stores n integers*/
String names[]=new String[n];
[Link]("Enter "+n+" students marks and names");
for(int i=0;i<[Link];i++)
{ /**i is used as index number*/

names[i]=[Link]();
}
[Link]("students securing 40-60");
for(int i=0;i<[Link];i++)
{
if(marks[i]>=40 && marks[i]<=60)
{
[Link](names[i]+" "+marks[i]);
}
}
[Link]("students securing >=80");
for(int i=0;i<[Link];i++)
{
if(marks[i]>=80)
{
[Link](names[i]+" "+marks[i]);
}
}
}
}
import [Link].*;
class Arrays_P4
{
static void main()
{
Scanner sc=new Scanner([Link]);
char a[]=new char[20];/**stores 20 chars*/
int c1=0;/**count uppercase*/
int c2=0;/**count lowercase*/
int c3=0;/**count digits*/
int c4=0;/**count symbols*/
[Link]("Enter 20 characters");
for(int i=0;i<[Link];i++)/**i - index number*/
{
a[i]=[Link]().charAt(0);
if([Link](a[i]))
c1++;
else if([Link](a[i]))
c2++;
else if([Link](a[i]))
c3++;
else
c4++;
}
[Link]("Count of uppercase="+c1);
[Link]("Count of lowercase="+c2);
[Link]("Count of digits="+c3);
[Link]("Count of symbols="+c4);
}
}
import [Link].*;
class Arrays_P5
{
static void main()
{
Scanner sc=new Scanner([Link]);
int a[]=new int[10];/**stores the average speed*/
int c1=0;/** to count the range 5 to 20*/
int c2=0;/** to count the range 21 to 50*/
int c3=0;/** to count the range 51 to 75*/
int c4=0;/** to count the range 76 to 89*/
int c5=0;/** to count the range 90 to 100*/
[Link]("Enter the average speed of 10 cyclists");
for(int i=0;i<[Link];i++)/**i-index number*/
{
a[i]=[Link]();
if(a[i]>=5 && a[i]<=20)
c1++;
else if(a[i]>=21 && a[i]<=50)
c2++;
else if(a[i]>=51 && a[i]<=75)
c3++;
else if(a[i]>=76 && a[i]<=89)
c4++;
else if(a[i]>=90 && a[i]<=100)
c5++;
}
[Link]("Count of 5 to 20 speed="+c1);
[Link]("Count of 21 to 50 speed="+c2);
[Link]("Count of 51 to 75 speed="+c3);
[Link]("Count of 76 to 89 speed="+c4);
[Link]("Count of 90 to 100 speed="+c5);
}
}
import [Link].*;
class Arrays_P6
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[]=new int[10];/**stores 10 integers*/
[Link]("Enter 10 integers");
int search;/**search element*/
for(int i=0;i<[Link];i++)/**i-index number*/
{
a[i]=[Link]();
}
[Link]("Enter an element to search");
search=[Link]();
boolean found=false;/**flag variable*/
for(int i=0;i<[Link];i++)
{
if(a[i]==search)
{
found=true;
[Link]("found at "+i+" position");
break;
}
}
if(found==false)
[Link]("data not found");
}
}
import [Link].*;
class Arrays_P7
{
void main()
{
Scanner sc=new Scanner([Link]);
double marks[]=new double[10];/**stores 10 marks*/
String names[]=new String[10];/**stores 10 names*/
String search;/**name to search*/
[Link]("Enter 10 names and % marks");
for(int i=0;i<[Link];i++)/**i-index number*/
{
names[i]=[Link]();
marks[i]=[Link]();
}
[Link]("Enter a name to be searched");
search=[Link]();
boolean found=false;/**flag variable*/
for(int i=0;i<[Link];i++)
{
if([Link](names[i]))
{
found=true;
[Link]("Name :"+names[i]);
[Link]("Marks :"+marks[i]);
break;
}
}
if(found==false)
[Link]("Data not found");
}
}
import [Link].*;
public class Arrays_P8
{
static void main() {
Scanner sc=new Scanner([Link]);
char a[]={'Y','W','U','S','P','N','L','K','G','C','A'};
[Link]("Enter the character to search");
char search= [Link]().charAt(0); /**stores a character*/
int lb=0; /**lower boundary*/
int ub=[Link]-1; /**upper boundary*/
int m; /**middle index*/
boolean found=false; /**flag variable*/
while(lb<=ub)
{
m=(lb+ub)/2;
if(search<a[m])
lb=m+1;
else if(search>a[m])
ub=m-1;
else if(search==a[m])
{
found=true;
[Link]("Character found at index number "+m);
break;
}
}
if(found==false)
[Link]("Data not found");
}
}
import [Link].*;
class Arrays_P9
{
void main()
{
Scanner sc=new Scanner([Link]);
double temp[]=new double[10];/** stores the temperatures*/
[Link]("Enter 10 temperatures");
for(int i=0;i<[Link];i++) /**i-index number*/
{
temp[i]=[Link]();
}
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(temp[j]<temp[j+1])
{
double t=temp[j]; /**t-temporary variable*/
temp[j]=temp[j+1];
temp[j+1]=t;
}
}
}
[Link]("Temperatures in descending order");
for(int i=0;i<[Link];i++)
[Link](temp[i]);
}
}
import [Link].*;
class Arrays_P10
{
void main()
{
Scanner sc=new Scanner([Link]);
String n[]=new String[10];/** stores Organization names*/
[Link]("Enter 10 Organization names");
for(int i=0;i<[Link];i++) /**i-index number*/
{
n[i]=[Link]();
}
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(n[j].compareToIgnoreCase(n[j+1])>0)
{
String t=n[j]; /**t-temporary variable*/
n[j]=n[j+1];
n[j+1]=t;
}
}
}
[Link]("Organization names in ascending order");
for(int i=0;i<[Link];i++)
[Link](n[i]);
}
}
import [Link].*;
class Arrays_P11
{
void main()
{
Scanner sc=new Scanner([Link]);
int uid[]=new int[10];/**stores 10 uid numbers*/
[Link]("Enter 10 UID numbers");
for(int i=0;i<[Link];i++)/**i-index number*/
uid[i]=[Link]();
int min;/**least number*/
int pos;/**position of least number*/
int t;/**temporary variable*/
for(int i=0;i<[Link]-1;i++)
{
min=uid[i];
pos=i;
for(int j=i+1;j<[Link];j++)
{
if(uid[j]<min)
{
min=uid[j];
pos=j;
}
}
t=uid[i];
uid[i]=min;
uid[pos]=t;
}
[Link]("UID numbers in order");
for(int i=0;i<[Link];i++)
[Link](uid[i]);
}
}
import [Link].*;
class Arrays_P12
{
void main()
{
Scanner sc=new Scanner([Link]);
String n[]=new String[10];/** stores names*/
int m[]=new int[10];/**stores marks*/
[Link]("Enter 10 names and marks");
for(int i=0;i<[Link];i++) /**i-index number*/
{
n[i]=[Link]();
m[i]=[Link]();
}
for(int i=0;i<[Link]-1;i++)
{
for(int j=0;j<[Link]-i-1;j++)
{
if(m[j]>m[j+1])
{
int t=m[j]; /**t-temporary variable*/
m[j]=m[j+1];
m[j+1]=t;
String t1=n[j]; /**t1-temporary variable*/
n[j]=n[j+1];
n[j+1]=t1;
}
}
}
[Link]("Names Marks");
for(int i=0;i<[Link];i++)
[Link](n[i]+" "+m[i]);
}
}
import [Link].*;
class Arrays_P13
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int min=a[0][0];/**stores minimum value*/
[Link]("Array in matrix format");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
[Link](a[i][j]+" ");
if(a[i][j]<min)
min=a[i][j];
}
[Link]();
}
[Link]("Minimum value is "+min);
}
}
import [Link].*;
class Arrays_P14
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[4][4];/**stores 9 values*/
[Link]("Enter 16 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int sum1=0;/**sum of primary diagonal values*/
int sum2=0;/**sum of secondary diagonal values*/
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
[Link](a[i][j]+"\t");
if(i==j)
sum1=sum1+a[i][j];
else if(i+j==3)
sum2=sum2+a[i][j];
}
[Link]();
}
[Link]("sum of primary diagonal values="+sum1);
[Link]("sum of secondary diagonal values="+sum2);
}
}
import [Link].*;
class Arrays_P15
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
}
}
int sum=0;/**sum of each row*/
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
sum=sum+a[i][j];
}
[Link]("Sum of row "+(i+1)+"="+sum);
sum=0;
}
}
}
import [Link].*;
class Arrays_P16
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
int count=0;/**count of even numbers*/
int p=1;/**product of odd values*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
if(a[i][j]%2==0)
count++;
else
p=p*a[i][j];
}
}
[Link]("Count of even numbers="+count);
[Link]("Product of oddn numbers="+p);

}
}
import [Link].*;
class Arrays_P17
{
void main()
{
Scanner sc=new Scanner([Link]);
int a[][]=new int[3][3];/**stores 9 values*/
int count=0;/**count of 0's*/
[Link]("Enter 9 values");
for(int i=0;i<[Link];i++)/**i-row index*/
{
for(int j=0;j<[Link];j++)/**j-column index*/
{
a[i][j]=[Link]();
if(a[i][j]==0)
count++;
}
}
if(count>(9-count))
[Link]("Sparse Matrix");
else
[Link]("Not a Sparse Matrix");
}
}

Common questions

Powered by AI

The program in Arrays_P1 iterates through the array 'a' and checks if each element is divisible by 7 or ends with 7 by using the condition 'a[i] % 7 == 0 || a[i] % 10 == 7'. It maintains a count for such numbers using the variable 'count'. Additionally, it calculates the product of all even integers in the array using the variable 'p', initialized to 1 and multiplied by each even integer found. It also prints negative numbers from the array .

Sorting temperatures in descending order in Arrays_P9 is intended to facilitate easy analysis of the highest to lowest temperatures. This is accomplished using the bubble sort algorithm, where each temperature is compared with its successor and swapped if it is smaller. This continues iteratively, ensuring the largest elements migrate to the beginning of the array .

Classifying average speeds into predefined ranges in Arrays_P5 helps to perform statistical analysis by categorizing data into meaningful segments. This method provides insights into patterns, helps identify performance levels, and supports decision-making processes such as setting performance benchmarks or tailoring training programs for cyclists .

Arrays_P16 differentiates even numbers by checking if a number modulo 2 equals zero, incrementing the count. Odd numbers are identified when this condition fails, and their product is calculated by multiplying them with a cumulative variable. This dual approach allows the separation of logic for handling even and odd integers within the matrix, facilitating specific analyses like diversity or parity checks within data sets .

The search functionality in Arrays_P7 looks for a student's name by iterating through the array of names. It uses the equalsIgnoreCase method to allow for case-insensitivity during comparison. If a match is found, it retrieves and displays the corresponding marks, setting the flag 'found' to true. If no match is found after checking all elements, it outputs a 'data not found' message .

Arrays_P13 determines the minimum value in a 3x3 matrix by initializing the minimum variable with the first element of the array. It then iterates through all matrix elements, updating the minimum whenever a smaller element is found. This method ensures that the smallest value at any matrix location is captured .

Arrays_P6 searches for an integer within an array by iterating through each element and comparing it with the search value. If a match is found, it prints the position of the integer and exits the loop using a boolean flag 'found' to break out of the loop early. If the loop completes without finding the integer, it prints 'data not found' .

Arrays_P4 counts characters by incrementing separate counters for uppercase, lowercase, digits, and symbols. It uses conditional checks with Character class methods like isUpperCase, isLowerCase, and isDigit. Distinguishing these types allows programs to perform operations like data parsing, validation, or formatting based on specific subsets of characters, which is essential in text processing and security .

Arrays_P14 calculates the sum of primary diagonal elements where the row index equals the column index (i.e., i == j), and the secondary diagonal where the sum of row and column indices equals 3 (i.e., i + j == 3). The sum of these diagonals often provides insights into key characteristics of matrices, such as assessing whether a matrix is singular or identifying specific symmetry properties .

The bubble sort algorithm in Arrays_P10 organizes organization names in ascending alphabetical order. It repeatedly steps through the array, compares adjacent elements, and swaps them if they are in the wrong order. This process is carried out iteratively until the entire array is sorted, ensuring each pass places the next largest item in its correct position .

You might also like