2 Replies - 2792 Views - Last Post: 15 January 2010 - 08:36 PM Rate Topic: -----

#1 zexxa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-January 10

Min and Max of an array as a function(C)

Posted 15 January 2010 - 08:18 PM

I'm finishing up a program for a class and I've run into a problem. The program is supposed to return the min and max of an array of numbers then decrypt the message(subtract the integers by 1) then print the ascii message. I can get it to decrypt and print the code, the only problem is that the find_min_max function isn't doing anything. My program is returning -1234 1234 and my message. I can't find what's wrong. Help?


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{
  int min;
  int max;
  char str[100];

}Values;


//Prototypes																	

void find_min_max(int [], int n, int *, int *);
void find_string(int nums[], int n, char s[]);


//BEGINNING OF MAIN															 
main()
{
  Values vals;
  int *nums;
  int n, i, min, max;
  char s[100];


  printf("How many integers are you inputing: " );
  scanf("%d", &n);

  //Allocating memory														   
  nums = (int*)malloc(sizeof(int) * n);

  printf("Enter the integers: ");
  for(i=0; i<n; i++){
	scanf("%d", &nums[i]);}

  //vals.min = min;															 
  //vals.max = max;															 
  //strcpy(vals.str, s);														

  find_min_max(nums, n, &min, &max);
  find_string(nums, n, s);

  vals.min = min;
  vals.max = max;
  strcpy(vals.str, s);

  printf("%d\n", vals.min);
  printf("%d\n", vals.max);
  printf("%s\n", vals.str);

}

//THIS IS THE END OF MAIN

void find_min_max(int nums[], int n, int *min, int *max)
{
  int i;
  *max = -1234;
  *min = 1234;
  //Create a loop to check for min and max									  
  for(i = 0; i < n; i++){
	if(nums[i] < *min)
	  *min == nums[i];}
  for(i = 0; i < n; i++){
	if(nums[i] > *max)
	  *max == nums[i];}
}

void find_string(int nums[], int n, char s[])
{
  int i;

  //Create a loop to decrypt													
  for(i = 0; i < n; i++){
	s[i] = --nums[i];}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Min and Max of an array as a function(C)

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Min and Max of an array as a function(C)

Posted 15 January 2010 - 08:30 PM

When you try to reassign it if it's lower/higher, you're using the equality operator instead of the assignment one:

for(i = 0; i < n; i++){
	if(nums[i] < *min)
	  *min == nums[i];} //here
  for(i = 0; i < n; i++){
	if(nums[i] > *max)
	  *max == nums[i];} //and here


Was This Post Helpful? 1
  • +
  • -

#3 zexxa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-January 10

Re: Min and Max of an array as a function(C)

Posted 15 January 2010 - 08:36 PM

OMG I love you. I've been trying to figure this out for almost an hour. I never would have thought that was the problem. LOL. Thank you so much!









View PostKYA, on 15 Jan, 2010 - 07:30 PM, said:

When you try to reassign it if it's lower/higher, you're using the equality operator instead of the assignment one:

for(i = 0; i < n; i++){
	if(nums[i] < *min)
	  *min == nums[i];} //here
  for(i = 0; i < n; i++){
	if(nums[i] > *max)
	  *max == nums[i];} //and here


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1