2 Replies - 250 Views - Last Post: 03 March 2018 - 05:52 AM Rate Topic: -----

#1 Noel12345678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 18

Math with Array's

Posted 02 March 2018 - 11:23 AM

Here's a test question. I do NOT understand why the LETTERS in the array change the answer. Please explain.

If the array is "ABCDE" the answer is -2 (try it...)
...but if "CABDE" the answer becomes 1 (and my IDE reproduces this)

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

int main(void)
{
	char s[11]="ABCDE";
	
	printf("the value of s = %d, and s+2 = %d, and s[0] = and s[2]= \n\n", s, s+2, s[0], s[2]);
	
	
	strcat(s+2, "ABCDE");
	printf("%s", char s);
	printf("%d", s[0]-s[2]);
	return 0;
}


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

int main(void)
{
	char s[11]="CABDE";
	
	printf("the value of s = %d, and s+2 = %d, and s[0] = and s[2]= \n\n", s, s+2, s[0], s[2]);
	
	
	strcat(s+2, "CABDE");
	printf("%s", char s);
	printf("%d", s[0]-s[2]);
	return 0;
}

This post has been edited by ndc85430: 02 March 2018 - 11:25 AM
Reason for edit:: Added code tags. Please do this yourself in future.


Is This A Good Question/Topic? 0
  • +

Replies To: Math with Array's

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14096
  • View blog
  • Posts: 56,491
  • Joined: 12-June 08

Re: Math with Array's

Posted 02 March 2018 - 11:43 AM

Have you considered that letters are value represented, in ascii, as numbers.

https://www.asciitable.com/


So A corresponds to 65.. and C to 67.
Was This Post Helpful? 2
  • +
  • -

#3 Noel12345678   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-February 18

Re: Math with Array's

Posted 03 March 2018 - 05:52 AM

THAT WAS IT!

Thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1