Trouble with an array program in C

  • Thread starter Thread starter dect117
  • Start date Start date
  • Tags Tags
    Array Program
Click For Summary

Discussion Overview

The discussion revolves around a homework problem requiring the creation of a function in C to determine if three arrays of integers are identical. Participants explore various coding approaches, syntax issues, and logic simplifications related to the problem.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant expresses uncertainty about using a for-loop for comparison, suggesting it feels messy but is unsure of alternatives.
  • Another participant points out that the assignment statements should use a single equals sign (x=1 and x=2) instead of a double equals sign (x==1 and x==2).
  • A suggestion is made to simplify the logic by using a Boolean variable to track if a difference is found, rather than using an else if statement.
  • One participant encourages considering other iteration methods and matrix comparison techniques, especially if the problem involves matrix algebra.
  • Another participant recommends hardcoding array values or reading them from a file for easier testing, suggesting this could save time during program development.

Areas of Agreement / Disagreement

Participants generally agree on the need for syntax corrections and logic simplifications, but there are multiple approaches suggested for solving the problem, indicating that no single solution has been established as the best method.

Contextual Notes

Some participants mention alternative methods and improvements without resolving the initial issues in the code, indicating that the discussion remains focused on refining approaches rather than reaching a definitive solution.

dect117
Messages
25
Reaction score
1

Homework Statement


Write a function that finds if three arrays of integers are identical. The arrays are identical when they have the same values at the same indices. Below are two samples outputs.
1.PNG
2.PNG

Homework Equations


N/A

The Attempt at a Solution


I decided to compare the arrays using a for-loop. I feel like that's a messy way to go about it but I'm not really sure how else I can make it work. Here's my code.
Code:
#include <stdio.h>
#define SIZE 5
int main () {
    int i, x, arr1[SIZE], arr2[SIZE], arr3[SIZE];
    printf ("Array A: ");
    for (i=0; i<SIZE; i++)
        scanf ("%d", &arr1[i]);
    printf ("Array B: ");
    for (i=0; i<SIZE; i++)
        scanf ("%d", &arr2[i]);
    printf ("Array C: ");
    for (i=0; i<SIZE; i++)
        scanf ("%d", &arr3[i]);
    for (i=0; i<SIZE; i++){
        if (arr1[i]==arr2[i] && arr2[i]==arr3[i]) {
            x==1;
        }
        else if(!(arr1[i]==arr2[i] && arr2[i]==arr3[i])) {
            x==2;
            break;
        }
    }
    if (x==1)
        printf ("\nThese arrays are identical!");
    else if (x==2)
        printf ("\nThese arrays are not identical!");
    return 0;
}
It prints "These arrays are identical!" regardless of whether or not that is true. I'm not understanding why it won't work.
 

Attachments

  • 1.PNG
    1.PNG
    2.3 KB · Views: 759
  • 2.PNG
    2.PNG
    2.5 KB · Views: 420
Physics news on Phys.org
The statements x==1 and x==2 should be x=1 and x=2.

Also:
the "else if" part is not needed. It can be a simple "else".

I would have declared:
Code:
bool bDifferenceFound;

Then before the comparison loop, I would say:
Code:
bDifferenceFound = false;

Then in the loop:
Code:
  bDifferenceFound = (arr1 != arr2) || (arr2 != arr3);
  if(bDifferenceFound) break;
 
Last edited:
Look at your syntax for the assignment operators...
 
Wow, thanks a lot guys! I have to pay more attention.
 
Since your Question 3 requested alternate forms:
Consider using Boolean variables and tests to simplify logic statements as detailed in response #2.

Since you wrote a "For" loop to input the values into arrays, it seems natural to use more For loops to evaluate the arrays but consider other iteration and matrix comparison methods since your code locates the first mismatch, sets Boolean "false", then dumps the loop.

If your math includes matrix algebra, consider methods to determine if Mat_A = Mat_B for values contained in same dimension matrices.
(Forgive me if I exceed the boundaries of your thread as I'm new to answering homework questions online. --Norm)
 
It would be easier to hardcode some array values or take them from a file while testing your program - and faster since you won't have to insert values.
Then change it back later.
You could also just check if they are not the same as suggested earlier - and if you never hit that break then they ought to be the same right?
Know I'm a little late but wanted to give some advice for future assignments - setting up programs for testing will save lots and lots of time.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K