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
xiromdimAsked:
Who is Participating?
 
Infinity08Connect With a Mentor Commented:
You only need to keep the start index. Then you iterate over the array until you find equal values. And then you know the end index.
0
 
ozoCommented:
Is this homework?
what are you having difficulty with?
0
 
xiromdimAuthor Commented:
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...
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Infinity08Commented:
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 ...
0
 
xiromdimAuthor Commented:
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

0
 
Infinity08Commented:
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.
0
 
xiromdimAuthor Commented:
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....
0
 
xiromdimAuthor Commented:
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

0
 
Infinity08Commented:
Looks good.

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

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.