| | |
Problem with filling and searching arrays.
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2008
Posts: 2
Reputation:
Solved Threads: 0
Hello everyone. Like so many others, I am new to programming. I am taking a course on Programming in C, and have hit a number of speed bumps.
The assignment I am currently stumped on is:
Let arr be an array of 20 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest item.
Here is what I have for code so far. Please help me figure out what the heck I am doing wrong. This is all like ancient Greek to me!!!
The assignment I am currently stumped on is:
Let arr be an array of 20 integers. Write a program that first fills the array with up to 20 input values and then finds and displays both the subscript of the largest item in arr and the value of the largest item.
Here is what I have for code so far. Please help me figure out what the heck I am doing wrong. This is all like ancient Greek to me!!!
C Syntax (Toggle Plain Text)
/*Finds the largest integer value in an array and displays the value and the subscript*/ #include <stdio.h> #define MAX_INT 20 #define SENT -99 int fill_to_sent(SENT, int arr[], int *arr_sizep); int get_max(int *const arr[], int n); int get_sub(int *const arr[], int n); int main(void) { int arr[MAX_INT], /*data list */ in_use, /*number of elements filled */ cur_large, /*largest value so far */ large_sub; /*subscript of largest value */ /*Fills array with up to 20 integers */ fill_to_sent(SENT, arr[], &in_use); /*Finds largest integer in array */ get_max(const arr[], in_use); /*Finds subscript value for largest integer*/ get_sub(const arr[], in_use); printf("The largest integer entered is %d and the subscript value is %d\n", &cur_large, &large_sub); return(0); } /*Gets integers to fill arr until the sentinel value is encountered. *Pre: SENT and MAX_INT are defined and MAX_INT is the declared size of arr.*/ int fill_to_sent(SENT, /*input - end of data value in input list */ int arr[], /*output - array of data */ int &in_use);/*output - number of array elements filled */ { int data, i, status; printf("Please enter up to 20 integers.\n"); printf("When you are finished entering integers,\n"); printf("enter %d to show you are done.\n", &SENT); i = 0; status = scanf("%d", &data); while (status == 1 && data != SENT && i < MAX_INT){ arr[i] = data; ++i; status = scanf("%d", &data); } /*Issues error message for incorrect data */ if (status != 1) { printf("\n*** Error in data format ***\n"); printf("*** Using first %d integers entered ***\n", i); } else if (data != SENT) { printf("\n*** Error: too many integers were entered ***\n"); printf("*** Using first %d integers entered ***\n", i); } *arr_sizep = i; } /* Returns the largest integer in the used array elements */ int get_max(int *const arr[], /*input - list of intergers */ int arr_sizep) /*input - number of elements to examine */ { int i, cur_large; /*largest value so far */ /*Initial array element is largest so far */ cur_large = arr[0]; /*Compare each remaining list element to the largest so far and save the larger */ for (i = 1; i < arr_sizep; ++i) if (arr[i] > cur_large) cur_large = arr[i]; return (cur_large); } /*Returns the subscript value of the largest integer */ int get_sub(int *const arr[], int arr_sizep) { int i, large_sub; large_sub = 0; for (i = 1; i < arr_sizep; ++i) if (arr[i] > arr[large_sub]) large_sub = i; return (large_sub); }
Even i'm prettty much new to C programming, but i'll try my best to help.
One of the errors is the illegal use of 'address of' operator. When you use the "&" in ur printf statements, it'll print the address of the variable and not it's value.
And if u r passing the address of a variable in a function, there should be a pointer variable which can hold the address value. the prototype and the function definition of fill_to_sent() do not match.
Perhaps I'm underestimating ur problem, but ur code seems a bit too complex for a pretty simple problem.
The following code takes input from the user and calculates the biggest element in the array and prints the subscript value as well.
Lemme know if i've missed something.
Cheers!
One of the errors is the illegal use of 'address of' operator. When you use the "&" in ur printf statements, it'll print the address of the variable and not it's value.
And if u r passing the address of a variable in a function, there should be a pointer variable which can hold the address value. the prototype and the function definition of fill_to_sent() do not match.
Perhaps I'm underestimating ur problem, but ur code seems a bit too complex for a pretty simple problem.
The following code takes input from the user and calculates the biggest element in the array and prints the subscript value as well.
C Syntax (Toggle Plain Text)
#include<stdio.h> int main() { int a[20],i,n,big,subscript=0; printf("enter the no of elements (MAX IS 20!)\n"); scanf("%d", &n); printf("Enter the elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); big=a[0]; for(i=1;i<n;i++) { if(a[i] > big) { big=a[i]; subscript=i; } } printf("Biggest element is %d\n", big); printf("Subscript is %d\n", subscript); getch(); return 0; }
Lemme know if i've missed something.
Cheers!
Last edited by devnar; Sep 6th, 2008 at 4:48 pm.
![]() |
Other Threads in the C Forum
- Previous Thread: Help in Postfix to Binary Tree Representation
- Next Thread: do/while not reading the condition right
Views: 534 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C
#include * .net append array arrays asterisks binarysearch calculate changingto char character cm command copyimagefile cprogramme creafecopyofanytypeoffileinc database directory dynamic execv feet fgets file fork forloop framework function functions givemetehcodez grade graphics gtkwinlinux hacking histogram homework include incrementoperators input intmain() iso kernel keyboard km lazy license linked linkedlist linux list lists locate logical_drives looping loopinsideloop. lowest matrix microsoft mqqueue mysql number oddnumber odf opensource overwrite owf pdf performance pointer pointers posix probleminc process program programming radix recursion recv recvblocked research reversing scanf scripting segmentationfault sequential socket spoonfeeding standard string student systemcall testing threads turboc unix user variable wab whythiscodecausesegmentationfault windowsapi





