array programming practice exercises

Thread Starter

anukalp

Joined Jul 28, 2018
158
Assuming that you're learning programming for practical (rather than academic) reasons, here are some general thoughts:
Thanks bogosort for your excellent explanation.

I will soon start a programming for 8051 microcontroller. I have just started reading about it.

I wanted to learn the c programming very well before starting embedded c programming, This is reason I was solving some common practice exercise of c programming.
 

ArakelTheDragon

Joined Nov 18, 2016
1,366
I do not mean to critisize you, I just also consider he needs this for academical and learning purposes.
Who said the point was for the OP to understand how sorting worked? He wrote a program that looked for duplicates. I pointed out that his brute force algorithm was inefficient on larger arrays and he should consider sorting the array before looking for duplicates.

Many people who use qsort don't understand how to implement the algorithm or why it's better or worse than any other sorting algorithm written by some of the Greats in CS. We stand on the shoulders of giants in this area and I see little point in trying to reverse engineer what they did if all the OP wanted to do was scan an array for duplicate numbers more efficiently.

I don't know, or care, how most functions were implemented; I just use them. When I write code, I'm trying to accomplish whatever it is I wanted to accomplish; not understand implementation details of the functions I'm using to do it.
 

xox

Joined Sep 8, 2017
936
Something like this:
Code:
char* reverse_string(char *str)
{
  char temp;
  size_t len = strlen(str) - 1;
  size_t stop = len/2;
  size_t i,k;

  for(i = 0, k = len; i < stop; i++, k--)
  {
  temp = str[k];
  str[k] = str[i];
  str[i] = temp;
  }
  return str;
}

Beware, the error of the fencepost!

Code:
#include <string.h>
#include <stdio.h>

char* reverse_string(char *str)
{
  char temp;
  size_t len = strlen(str) - 1;
  printf("Actual length of string: %zu\n", strlen(str));
  printf("Adjusted length of string: %zu\n", len);
  size_t stop = len/2;
  size_t i,k;
  for(i = 0, k = len; i < stop; i++, k--)
  {
  temp = str[k];
  str[k] = str[i];
  str[i] = temp;
  }
  return str;
}

int main(void)
{
  char txt[] = "";
  reverse_string(txt);
}
user@xox ~/app/test $ launch test
Actual length of string: 0
Adjusted length of string: 18446744073709551615
~/app/bin/launch: line 14: Segmentation fault ./$1
 
Last edited:
Top