I'm stuck with this method which should "find the largest integer in an array passed as a parameter and also output the count of the maximum value", not sure what I'm doing wrong.
Thanks
public class q3 {
public static void main(String[] args) {
int[] arraY = {20, 20, 10, 19, 1, 4, 25, 19, 1, 11, 15, 11, 29, 27, 7, 22, 14, 26, 2, 6, 18};
findMax(arraY);
}
public static void findMax(int[] arraY) {
int max_value = arraY[0];
int countMax = 0;
for (int i = 1; i < arraY.length; i++) {
if (arraY[i] > max_value) {
max_value = arraY[i];
}
for (i = 0; i < arraY.length; i++) {
if (arraY[i] == max_value) {
//System.out.println(array[i]);
//System.out.println(max_value);
countMax++;
}
}
}
System.out.println("The maximum value is: " + max_value);
System.out.println("The count is: " + countMax);
}
}
edit: was an issue with scope, have rearranged the formatting and it now returns the correct result
code updated below
public class q3 {
public static void main(String[] args) {
int[] arraY = {20, 20, 10, 19, 1, 4, 25, 19, 1, 11, 15, 11, 29, 27, 7, 22, 14, 26, 2, 6, 18};
//int[] arraY = {30, 9, 20, 9};
findMax(arraY);
}
public static void findMax(int[] arraY) {
int max_value = arraY[0];
int countMax = 0;
for (int i = 1; i < arraY.length; i++) {
//System.out.println(arraY[i]);
if (arraY[i] > max_value) {
max_value = arraY[i];
}
}
for (int i = 0; i < arraY.length; i++) {
if (arraY[i] == max_value) {
//System.out.println(arraY[i]);
//System.out.println(max_value);
countMax++;
}
}
System.out.println("The maximum value is: " + max_value);
System.out.println("The count is: " + countMax);
}
}
I'm doing a school project where I need to ask for a year and a month to be printed like a calendar. The problem is that when I debug it shows the whole year and not just the chosen month.
What changes do I have to do? Hope you can help me!
This is my code:
int SetFirstDay(int year)
{
int d;
int d1, d2, d3;
d1 = (year - 1.) / 4.0;
d2 = (year - 1.) / 100.;
d3 = (year - 1.) / 400.;
d = (year + d1 - d2 + d3) % 7;
return d;
}
int main()
{
int day, month, year, ndays, DayWeek = 0, FirstDay;
do
{
printf("\nEnter the desired year [2000 - 2099]\n");
scanf_s(" %d", &year);
} while (year < 2000 || year > 2100);
do
{
printf("\nEnter the desired month [1 - 12]\n");
scanf_s(" %d", &month);
} while (month < 1 || month > 12);
char *months[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
int DayMonth[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
DayMonth[1] = 29;
FirstDay = SetFirstDay(year);
for (month = 0; month < 12; month++)
{
ndays = DayMonth[month];
printf("\n\n--------------%s-----------------\n", months[month]);
printf("\n Sun Mon Tue Wed Thu Fri Sat\n");
for (DayWeek = 0; DayWeek < FirstDay; DayWeek++)
{
printf(" ");
}
for (day = 1; day <= ndays; day++)
{
printf("%5d", day);
if (++DayWeek > 6)
{
printf("\n");
DayWeek = 0;
}
FirstDay = DayWeek;
}
}
printf("\n\n\n");
system("pause");
return 0;
}
Save the month value which comes from user input to an additional variable:
int monthTmp = month;
Then prefix all printf invokations with the following statement withing the "month" for loop:
for (month = 0; month < 12; month++)
{
// ...
if (monthTmp==month) printf(.........
It is not an optimized solution, but it is very straightforward.
I'm writing a function whose purpose is to change the value of struct variables. I'm getting error messages that say the expressions toward the end of my first function are not assignable. How, then, do I have the function change the values of the daate structure?
Here's my code:
#include <stdio.h>
#include <stdbool.h>
struct date
{
int month;
int day;
int year;
};
struct date dateUpdate (struct date *dait) {
struct date tomorrow;
int numberOfDays (struct date d);
if (dait->day != numberOfDays (*dait) ) {
tomorrow.day = dait->day + 1;
tomorrow.month = dait->month;
tomorrow.year = dait->year;
}
else if ( dait->month == 12 ) { //end of year
tomorrow.day = 1;
tomorrow.month = 1;
tomorrow.year = dait->year + 1;
}
else { //end of month
tomorrow.day = 1;
tomorrow.month = dait->month + 1;
tomorrow.year = dait->year;
}
&dait->day = tomorrow.day;
&dait->month = tomorrow.month;
&dait->year = tomorrow.year;
return *dait;
}
//Function to find the number of days in a month;
int numberOfDays (struct date d)
{
int days;
bool isLeapYear (struct date d);
const int daysPerMonth[12] =
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
if ( isLeapYear (d) && d.month == 2 )
days = 29;
else
days = daysPerMonth[d.month - 1];
return days;
}
}
int main (void) {
struct date daate;
daate.day = 21;
daate.month = 6;
daate.year = 2013;
dateUpdate(&daate);
printf("%d", daate.day);
}
The correct syntax (which you already used in the function) is the following
dait->day = tomorrow.day;
dait->month = tomorrow.month;
dait->year = tomorrow.year;
Take into account that instead of these three statements you could write simply
*dait = tomorrow;
dait is already a pointer, so &dait is a pointer to a pointer, and thus it has no fields to de-reference.
Solution: lose the & before dait.
I need to print the largest numbers in an array but my program keeps outputting the wrong numbers. What am i doing wrong?
int main(void)
{
int i, x,largest1,largest2, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
largest1 = 0;
largest2 = 0;
if (nums[i] > largest1)
largest1 = nums[i];
if (nums[x] > largest2)
largest2 = nums[x];
}
}
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", largest1, largest2);
system("pause");
}
Please check the data type of nums[i], its not an int. What you need is compare each value, say nums[i][x]
Do not initialize your largest number holder inside the loop. Otherwise, the largest number from previous iteration will be lost.
if the array contains all -ve numbers, initializing the highest number holder will 0 is faulty.
Check the below code[[ Tested on linux ]] for getting the idea. [For largest number only]
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void) {
int i, x,largest1, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
largest1 = INT_MIN;
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
if (nums[i][x] > largest1)
largest1 = nums[i][x];
}
}
printf("Largest number is %d\n", largest1);
return 0;
}
EDIT:
The code modification to include the second largest [and so on..] number display, is as per below logic
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
int main(void) {
int i, x,largest1,largest2, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
largest1 = INT_MIN;
largest2 = INT_MIN;
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
if (nums[i][x] > largest2)
largest2 = nums[i][x];
if ( largest2 > largest1)
{
largest2 = largest1;
largest1 = nums[i][x];
}
}
}
printf("Largest number is %d, second largest is %d\n", largest1, largest2);
return 0;
}
First of all, you should initialized largest1 and largest2 variables with the smallest possible integer values. Initializing them with 0 will give wrong result if all the numbers in your array are negative integers.
Also you should not re-initialize the largest1 and largest2 everytime you enter the inner loop.
I've corrected your code below. Please note that I've not tested the following code myself.
int i, x;
int largest1 = -32766;
int largest2 = -32767;
int nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
if (nums[i][x] > largest1) {
largest2 = largest1;
largest1 = nums[i][x];
} else if (num[i][x] > largest2) {
largest2 = num[i][x];
}
}
}
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", largest1, largest2);
system("pause");
Please make the following modifications. You have to access a 2-D array with 2 parameters to get that [x,y] value to compare.
int main(void) {
int i, x,largest1,largest2, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
largest1 = 0;
largest2 = 0;
if (nums[i][x] > largest1)
largest1 = nums[i][x];
if (nums[i][x] > largest2)
largest2 = nums[i][x];
}
}
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", largest1, largest2);
system("pause");
}
Hope it helps!
Please mark the answer right if its helpful and close the question.
You can move all the integer to a one dimensional array... then sort it. Then print the largest two element. Try this code...
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(void) {
int i, x,largest1,largest2, save[30], j=0, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
save[j++]=nums[i][x];
}
}
sort(save,save+j);
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", save[j-1], save[j-2]);
system("pause");
}
Try this :
int main(void)
{
int i, x,largest1,largest2, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
largest1 = 0;
largest2 = 0;
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
if (nums[i][x] > largest1)
largest1 = nums[i][x];
if (nums[i][x] > largest2 && nums[i][x] < largest1)
largest2 = nums[i][x];
}
}
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", largest1, largest2);
system("pause");
}
everyone has already provided you with syntactically correct program.
just a slight addition (I thought may be this is what u wanted according to your prog) :
if (nums[i][x] > largest1)
{
largest2 = largest1;
largest1 = nums[i][x];
}
if (nums[i][x] > largest2)
largest2 = nums[i][x];
then largest1 and largest2 will have ur desired values.
and yeah, don't forget to initialize your initial values to lowest limit of int first.
Try this solution.
Here we are assigning new max value to largest1 and if we are getting again new max value we will assign old max value to largest2 , which will become the second largest value.
int main(void)
{
int i, x,largest1,largest2, nums[5][4] = { { 5, 6, 8, 9 }, { 3, -55, 6, 89 }, { 1023, 43, -2, 0 }, { 0, 12, 45, 12 }, { -4, 901, 34, 294 } };
largest1 = 0;
for (i = 0; i < 5; i++)
{
for (x = 0; x < 4; x++)
{
largest1 = 0;
largest2 = 0;
if (nums[i][x] > largest1){
largest2 = largets1; // assign last max value to `largest2`, it will be second max
largest1 = nums[i][x]; // getting new max value
}
}
}
printf("\n 1st largest: %d \n 2nd largest: %d \n\n", largest1, largest2);
system("pause");
}