I am having a little trouble figuring out where my mistake is in my assignment. Everything runs fine until the very end where it is supposed to show the largest value in the array. Every time I run the code it always shows arr[19] as the largest. I cannot figure out what I am doing wrong. It is probably something simple but I am not seeing it.
I appreciate the help!
/*
* Author:
* Date: 14 April 2009
* Assignment: Lesson 4 Chapter 8 Project 5
* Class:
* Instructor:
* School:
*
* Purpose: Create an array named "arr" of type integer with 20 fields. Offer the user the ability to input
* values or offer to autofill the array. After the input is completed display the largest value and its subscript.
*
* Requirements: User input to fill as many array values as wanted or to select the autofill option.
* Output: Largest array value and its subscript
*/
#include <stdio.h>
#include <stdlib.h>
int arr[20]; /*Array used for integer population and manipulation*/
/*Function prototypes*/
int
main(void)
{/*Start Main Function*/
int counter, /*Counter used in for loops*/
tempInt, /*temp variable used for misc holder*/
largestInt, /*Holds largest int value from array*/
largestIntInd, /*Holds subscript value of largest integer value in array*/
optionChosen; /*Integer used to hold value of menu option chosen*/
/*Welcome user to program*/
printf("Welcome to Lesson 4\n");
printf("Please select an option below by entering its corresponding number\n");
printf("1) Manually enter values into the array\n");
printf("2) Have the array auto-filled with values\n");
printf("Enter option by inputting corresponding number-->(1|2)");
/*Do loop to take user input until it is validated as a correct input option*/
do
{/*Start of while loop*/
scanf("%d",&optionChosen);
if (optionChosen == 1 || optionChosen == 2)
break;
else
printf("%d is not a valid menu option. Please enter only 1 or 2->", optionChosen);
}/*End of while loop*/
while(1==1);
/*A switch statement is used so that future options may be added without any major code rewrites*/
switch(optionChosen) {/*Start of switch statement*/
case 1:
/*user input*/
printf("\n\nYou will be prompted to enter 20 integers.\nTo stop early enter -1\n");
for (counter = 0; counter < 20; counter++) /*For counter asks user for input to add to array. Breaks if user enters -1*/
{
printf("Please enter the value for index %d-->",counter);
scanf("%d", &tempInt);
if (tempInt == -1)
{
break;
}
else
{
arr[counter] = tempInt;
}
}
break;
case 2:
/*autofill array- */
for (counter = 0; counter < 20; counter++)
{
arr[counter] = rand();
}
break;
default:
printf("Somehow you broke me... why did you break me?");
}/*End of switch statement*/
/*Used for outputting all array values-commented out for final submission*/
for (counter = 0; counter < 20; counter++)
{
printf("%d%7.2c%d\n",counter,' ',arr[counter]);
}
largestInt = arr[0];
largestIntInd = 0;
for (counter = 0; counter < 20; counter++) /*For statement to loop through entire array and keep the largest values subscript number*/
{
if (arr[counter] >= largestInt)
{
largestIntInd = counter;
}
}
/*Displays the largest value and its subscript value*/
printf("The largest integer in the arry is in subscript %d with a value of %d.\n", largestIntInd, arr[largestIntInd]);
}/*End Main Function*/
Question
allanon09
Hi,
I am having a little trouble figuring out where my mistake is in my assignment. Everything runs fine until the very end where it is supposed to show the largest value in the array. Every time I run the code it always shows arr[19] as the largest. I cannot figure out what I am doing wrong. It is probably something simple but I am not seeing it.
I appreciate the help!
/* * Author: * Date: 14 April 2009 * Assignment: Lesson 4 Chapter 8 Project 5 * Class: * Instructor: * School: * * Purpose: Create an array named "arr" of type integer with 20 fields. Offer the user the ability to input * values or offer to autofill the array. After the input is completed display the largest value and its subscript. * * Requirements: User input to fill as many array values as wanted or to select the autofill option. * Output: Largest array value and its subscript */ #include <stdio.h> #include <stdlib.h> int arr[20]; /*Array used for integer population and manipulation*/ /*Function prototypes*/ int main(void) {/*Start Main Function*/ int counter, /*Counter used in for loops*/ tempInt, /*temp variable used for misc holder*/ largestInt, /*Holds largest int value from array*/ largestIntInd, /*Holds subscript value of largest integer value in array*/ optionChosen; /*Integer used to hold value of menu option chosen*/ /*Welcome user to program*/ printf("Welcome to Lesson 4\n"); printf("Please select an option below by entering its corresponding number\n"); printf("1) Manually enter values into the array\n"); printf("2) Have the array auto-filled with values\n"); printf("Enter option by inputting corresponding number-->(1|2)"); /*Do loop to take user input until it is validated as a correct input option*/ do {/*Start of while loop*/ scanf("%d",&optionChosen); if (optionChosen == 1 || optionChosen == 2) break; else printf("%d is not a valid menu option. Please enter only 1 or 2->", optionChosen); }/*End of while loop*/ while(1==1); /*A switch statement is used so that future options may be added without any major code rewrites*/ switch(optionChosen) {/*Start of switch statement*/ case 1: /*user input*/ printf("\n\nYou will be prompted to enter 20 integers.\nTo stop early enter -1\n"); for (counter = 0; counter < 20; counter++) /*For counter asks user for input to add to array. Breaks if user enters -1*/ { printf("Please enter the value for index %d-->",counter); scanf("%d", &tempInt); if (tempInt == -1) { break; } else { arr[counter] = tempInt; } } break; case 2: /*autofill array- */ for (counter = 0; counter < 20; counter++) { arr[counter] = rand(); } break; default: printf("Somehow you broke me... why did you break me?"); }/*End of switch statement*/ /*Used for outputting all array values-commented out for final submission*/ for (counter = 0; counter < 20; counter++) { printf("%d%7.2c%d\n",counter,' ',arr[counter]); } largestInt = arr[0]; largestIntInd = 0; for (counter = 0; counter < 20; counter++) /*For statement to loop through entire array and keep the largest values subscript number*/ { if (arr[counter] >= largestInt) { largestIntInd = counter; } } /*Displays the largest value and its subscript value*/ printf("The largest integer in the arry is in subscript %d with a value of %d.\n", largestIntInd, arr[largestIntInd]); }/*End Main Function*/Link to comment
Share on other sites
10 answers to this question
Recommended Posts