Reputation: 986
I am trying to take 3 arrays and add each combination of elements to see if any are equal to 91 and must make sure that the result has the red value smaller than the blue value smaller than the green value. I cannot for the life of me think of how to store so many instances and then refer to them at the end.
As you can see, I am missing code because I do not know how to proceed.
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void) {
int i = 0;
int j = 0;
int k = 0;
int redSize = 6;
int blueSize = 9;
int greenSize = 9;
int red[] = (9, 22, 21, 18, 34, 13);
int blue[] = (20, 60, 14, 17, 39, 16, 6, 33, 18);
int green[] = (40, 7, 51, 26, 8, 24, 12, 11, 27);
for(i; i < redSize; i++){
result = red[i] + blue[j] + green[k]
while (j < blueSize){
// need to iterate over each item in array "blue"
while(k < greenSize){
//same for green
}
}
if (result == 91 && red[i] < blue[i] && blue[i] < green[i]){
printf("The red value is %d, the blue value is %d, and the green value is %d", red[i], blue[i], green[i]);
}
return 0;
}
}
Upvotes: 0
Views: 652
Reputation: 1188
Or we could avoid addition everytime, by checking in the green loop for a particular value.
#include<stdio.h>
int main(void)
{
int red[] = {9, 22, 21, 18, 34, 13};
int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
int i = 0, j = 0, k = 0;
while(i < redSize){
while (j < blueSize){
kValue = 91 - red[i] - blue[j];
while(k < greenSize){
if(green[k] == kValue && red[i] < blue[j] && blue[j] < green[k])
{
printf("The red value is %d, the blue value is %d, and the green value is %d", red[i], blue[j], green[k]);
}
k++;
}
j++;
}
i++;
}
return 0;
}
Upvotes: 1
Reputation: 6950
From your brief on question, I think this may help you, Please comment if its not what you really need.
for(i=0 to redSize){
for(j=0 to blueSize){
for(k=0 to greenSize){
if(red[i]<blue[j]<green[k]){
if(91 == red[i]+blue[j]+green[k]){
printf(red[i],blue[j],green[k]);
}
}
}
}
}
Upvotes: 1
Reputation: 66194
Notes: Your array declarations were wrong, your loop scopes were not correct, and the loop organization could allow substantial skipping by testing for the less-than condition and eliminating entire sub-loops. There is no need to run through all the greens-to-blues comparisons for a red value already known to not be less than the current green.
Algorithm:
for each red value
for each blue value "greater" then the current red value
for each green value "greater" than the current blue value
if (red+blue+green)=91, the trio is a candidate.
After throwing out a lot of cruft, it should look something like this:
#include<stdio.h>
int main(void)
{
int red[] = {9, 22, 21, 18, 34, 13};
int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
int i,j,k;
for(i=0;i<sizeof(red)/sizeof(red[0]); ++i)
{
for (j=0;j<sizeof(blue)/sizeof(blue[0]);++j)
{
if (red[i] < blue[j])
for (k=0;k<sizeof(green)/sizeof(green[0]);++k)
{
if (blue[j] < green[k] && red[i]+blue[j]+green[k]==91)
printf("red:%d blue:%d green:%d\n", red[i], blue[j], green[k]);
}
}
}
return 0;
}
Output
red:18 blue:33 green:40
Upvotes: 2
Reputation: 6117
#include <stdio.h>
int main(void) {
int red[] = {9, 22, 21, 18, 34, 13};
int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
int r, g, b;
for (r = 0; r < (sizeof(red) / sizeof(int)); ++r) {
for (g = 0; g < (sizeof(green) / sizeof(int)); ++g) {
for (b = 0; b < (sizeof(green) / sizeof(int)); ++b) {
int s = red[r] + green[g] + blue[b];
if (s == 91 && red[r] < blue[b] && blue[r] < green[g]) {
printf("%d %d %d", red[r], blue[b], green[g]);
}
}
}
}
return 0;
}
Upvotes: 1
Reputation: 28830
You were not that far. A few glitches
i*j*k
elements.For instance: (I just fixed your program, without optimizing anything)
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main(void){
int i = 0;
int j = 0;
int k = 0;
int redSize = 6;
int blueSize = 9;
int greenSize = 9;
int red[] = {9, 22, 21, 18, 34, 13};
int blue[] = {20, 60, 14, 17, 39, 16, 6, 33, 18};
int green[] = {40, 7, 51, 26, 8, 24, 12, 11, 27};
for(i=0 ; i < redSize; i++){
for(j=0 ; j < blueSize; j++){
for(k=0 ; k < greenSize; k++){
int result = red[i] + blue[j] + green[k];
if (result == 91 && red[i] < blue[j] && blue[j] < green[k]){
printf("The red value is %d, the blue value is %d, and the green value is %d\n", red[i], blue[j], green[k]);
}
}
}
}
return 0;
}
Upvotes: 3