Link to home
Start Free TrialLog in
Avatar of xiromdim
xiromdimFlag for Greece

asked on

printing the different elements of 2 arrays

We have 2 arrays e.g.
int A[]={1,2,3,4,5,6,7,8,9,10},
     B[]={1,3,2,8,7,4,7,8,11,10};
I want to write a C program to print the different elements of the arrays in this way:
2: 2 3
down to
6: 6 4
9: 9 11
i.e. when we have continuous different elements,  print only the first and the last element of the sequence.
Thanks in advance
Avatar of ozo
ozo
Flag of United States of America image

Is this homework?
what are you having difficulty with?
Avatar of xiromdim

ASKER

well, it is a part of a homework...I will post the code I have written so far...I know it is not so dificult but I'm a bit confused...
Also, what's the question ? In order for us to help you, we need to know what you need help with :) We can't do the work for you ...
dear experts, I mean this: with the code below we take all the different elements of the 2 arrays. The output of this code is: 1: 2 3 \n 2: 3 2 \n 3: 4 8 and so on...
The question is how can I take this output:
1: 2 3 //when we have continuous diff. elements print only the first and the last
down to
5: 6 4
8:9 11

I hope , this time I have been more clear...
Jim
#include<stdio.h>
 
int main(){
    int A[]={1,2,3,4,5,6,7,8,9,10};
    int B[]={1,3,2,8,7,4,7,8,11,10};
    
    for(int i=0; i<10; i++){
            if (A[i]!=B[i]) 
               printf("%d: %d  %d \n",i, A[i], B[i]);
               }
    getchar();
    return 0;
}

Open in new window

You mean you want to print only elements 1 through 8 rather than 0 through 9 ? If so, you just have to adjust the for loop's start point and upper limit.

If that's not what you mean, then I'm afraid I don't understand your question.
I mean: first of all we want to print only the different elements. This is very easy...I did it (by the way the homework is about txt files, so the code with the 2 arrays is NOT the homework...)
But, if we have continuous different elements (e.g. A[1],A[2],A[3],A[4],A[5]) I want to print only A[1] (the first) and A[5] (the last), with the message "down to" (or whatever...).
The problem is:
if (A[i]!=B[i]), I must keep track of the previous elements (equal or not-equal).
If A[i-1]==B[i-1] then startIndex=i, else (i.e. A[i-1]!=B[i-1]) there is a startIndex somewhere before.
Also: if A[i]!=B[i], I have to examine (or better keep track) the previous elements, to check if i is the last index of a sequence of indices with diff. elements, so lastIndex=i and so on....
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well I made it!!! here is the code... thanks...
#include<stdio.h>
 
int main(){
    int A[]={1,2,3,4,3,6,7,8,9,10};
    int B[]={1,3,2,8,7,4,7,8,11,10};
    int start, end, tempIndex;
    int i=0;
    
    while(i<10){
            if (A[i]!=B[i]){   printf("%d: %d %d \n",i, A[i], B[i]);//first diff. element
               start=i;
               tempIndex=i+1;
               while(A[tempIndex]!=B[tempIndex]){//go on while a[i]!=b[i]
                     tempIndex++;
               }
               end=tempIndex-1;
               if (end-start>1){
                                
                                printf("down to\n");
                                printf("%d: %d %d \n",end, A[end],B[end]);
                                }
               i=end+1;                 
               }//if a[i]!=b[i]
               else i++;
               }
    getchar();
    return 0;
}

Open in new window

Looks good.

One thing though : properly aligning your code (consistently) will make it a lot easier to read and maintain.