Question:
C programming array help?
Nathan
2011-06-25 11:42:09 UTC
so am trying to write a fucntion that makes the first index of smallest int in the array , here is my funcction code

#include "my.h"


void smallarge(int x[])

{


int i;



printf("\n");

small = INT_MAX;
large = INT_MIN;


for( i=0; i{

if ( x[i] < small )
small = i;

}


printf( "%d\n" , i);


return;
}

but i'm pretty sure i'm doing it wrong , please help . thank you
Four answers:
Unca Alby
2011-06-25 12:14:12 UTC
First off, will all due respect, ignore Jonathan's advice. You DO need to have a positive value for the ARRAY variable, equal to the length of the array, but otherwise your "for" loop is absolutely correct.



But I do notice two problems.



It looks to me like you're confusing the INDEX of the smallest integer with the VALUE of the smallest integer. to wit:



if (x(i] < small)

small = i;



So, the first time into loop, i==0, and no matter what x[i], you set "small" to "i", so now "small" is 0.



So, for the rest of the loop, small == 0, and won't change unless you have a negative number in your array. If your array consists of all positive integers, "small" will always end up as 0.



What you need is an another variable. Keep one for the smallest value you've seen so far, and the other one for the loop index where that value was found.



Secondly, after you're through looping, you print your loop index variable, "i". Whatever for? BY DEFINITION, that variable should equal the value in ARRAY after going through the entire loop.



And, a minor nit -- you define "large" -- why? You certainly never use it.



In conclusion:



Set ARRAY equal to the actual length of the array.

Set a variable for the VALUE of the smallest integer seen so far (set to x[i])

Set a variable for the INDEX where that integer was seen (set to i)

When completed, print the index of the smallest integer (NOT i)
?
2011-06-25 12:09:26 UTC
Your for loop is correct if ARRAY is the size of the array. The problem is that ARRAY is not defined in the function smallarge, so there's really no telling what it is. Your program would be more correct if it was something like:



void SmallestElement(int x[], int MaxArray)

{



I don't honestly see any actual problem with this code. It should print out the index of the smallest element of the array. Not the smallest value, just the index.
SurferTim850
2011-06-25 13:32:47 UTC
Try this. It compares the values correctly. Then assigns the index correctly. Set small and large to position zero in the array. Don't know what INT_MAX is.



int small = 0;

int large = 0;



for( i=0; i
{



// compare the values in the array

if ( x[i] < x[small] )

// set the index of the array

small = i;



// compare value with a value

if ( x[i] > x[large] )

// set the index with an index

large = i;



}



printf( "First index of lowest value: %d\n",small);

printf( "Lowest value: %d\n",x[small]);



printf( "First index of highest value: %d\n",large);

printf( "Highest value: %d\n",x[large]);



ADD: After the for loop is complete, the value in small will be the index to the first occurrence of the lowest value in the x array. That is x[small]. Same with large and x[large].



Here is your question:

"function that makes the first index of smallest int in the array"



You posted this. Wasn't that the answer to your question?

Additional Details

that prints out the first index of smallest int in the array
?
2016-11-29 16:11:06 UTC
the 1st answer isn't too undesirable. i think of of i might make it slightly smaller and use isalpha() and strchr() might desire to be purifier. char *vowels="aeiouAEIUO"; int countvows(char *strptr, vowels) { int circumstances = 0; on an identical time as(*strptr) { if(isalpha(*strptr)) { // all individuals understand it somewhat is a-zA-Z now if(strchr(vowels,*strptr)) // it somewhat is a vowel circumstances++; strptr++; } // if } // on an identical time as consonants might desire to be an identical technique actually , diverse than the if() might desire to be: if(!strchr(vowels,*strptr)) // it somewhat is a consonant }


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...