Message from C, C++ talks

July 2019

— Ok, understand know(just don't know meaning of "consecutive")

— Give me your code

— Give me your code too

— Ok sorry Alexander!

— 

The exercise asks me to eliminate consecutive elements (1,2,7,8) and replace them with their sum (1+2 and 7+8)

— I love this chat

— Ok

— Struct elem
{
int info;
struct elem *next;
};

struct elem *readFile (FILE *fp, struct elem *top);
void printList (struct elem *top);
struct elem *writeFile (FILE *fp, struct elem *top);

int main()
{
struct elem *top=NULL;
FILE *fp;

fp=fopen("inputFile.txt","r");
top=readFile(fp,top);
fclose(fp);
printList(top);
fp=fopen("outputFile.txt","w");
writeFile(fp,top);
fclose(fp);

}
struct elem *newelem (int info)
{
struct elem *nodo;
nodo=(struct elem *)malloc(sizeof(struct elem));

if (nodo==NULL)
return NULL;
nodo->info=info;
nodo->next=NULL;
return nodo;
}
struct elem *insertList(struct elem *nodo, struct elem *top)
{
if (top==NULL)
return nodo;
top->next=insertLista(nodo, top->next);
return top;
}
struct elem *readFile (FILE *fp, struct elem *top)
{
int info;
struct elem *nodo;

while (fscanf(fp,"%d",&info)>0)
{
nodo=newelem(info);
top=insertLista(nodo,top);
}
return top;
}
void printList (struct elem *top)
{
while (top!=NULL)
{
printf ("%d -> ",top->info);
top=top->next;
}
printf ("NULL");
}
struct elem *writeFile (FILE *fp, struct elem *top)
{
int sum=0;
struct elem *prec, *curr, *tmp;

for (prec=top;prec!=NULL;prec=prec->next)
{
for (curr=prec->next;curr!=NULL;)
{
if (curr<prec && prec<curr)
{
tmp=curr;
curr=curr->next;
free(tmp);
}
else
curr=curr->next;
}
{
sum=sum+prec->info;
fprintf (fp,"%d ",sum);
}
}
return sum;
}

Message permanent page

— Are we need to handle smth like:
input: 1 2 4 6
3 4 6 -> 7 6 -> 13
output: 13
?

— The exercise also requires use of File, so I read from File, insert it in the list and the result I write it to File.

Message permanent page

— Check this...very basic implementation I wrote

— No Alessandro, consider this example:

Example:
1, 2, 5, 13, 7, 8, 23

Output (with sum subsequence):
3, 5, 13, 15, 23

Message permanent page

— tycho01

— Perfect, but I can't use other structures, only Linked LIST!

— Actually, sum=0 should be declared inside loop