Search the Community
Showing results for tags 'functions'.
-
Hello guys, Need your help with excel. Couldn't find out how to do next function, I want to move 5 characters from left side to the right side. Here is an example what I want to get. I have searched for some formulas like " =RIGHT(A2,LEN(A2)-FIND(" ",A2))&" "&LEFT(A2,FIND(" ",A2)-1) " But cannot get result. ADJR_Achkva from this I want to get > Achkhva_ADJR ADJR_Keda_HOC from this I want to get > Keda_HOC_ADJR ADJR_Khelvachauri_TV from this I want to get > Khelvachauri_TV_ADJR BATM_Batumi_Circus from this I want to get > Batumi_Circus_BATM IMRT_Chiatura_TV from this I want to get > Chiatura_TV_IMRT I want this solution cause I have 1000+ results like this. Thank you in advance.
- 2 replies
-
- excel
- excel function
-
(and 4 more)
Tagged with:
-
I understand the difference between passing by reference and passing by value. But i'm really confused on deciding when to pass by reference or when to pass by value. Some functions take in paramters by value where as some functions are passed in by reference, sometimes functions use a mixture of both passing by reference and passing by value. Really my question is how do you know when to pass by reference or when to pass by value. As an example I have this bubblesort program that has a few functions in, as you can see the less_eq and greater_eq pass in by value where as the swap function takes in a mixture, and the bubblesort function is just by reference. Can anyone maybe explain this to me. Thanks I do apolagize if i've worded this poorly. #include<stdio.h> #include<string.h> int less_eq(char x, char y) { return x<=y; } int greater_eq(char x, char y) { return x>=y; } void swap(char *s,int i, int j) { char temp=s[i]; s[i]=s[j]; s[j]=temp; } void bubblesort (char *s, int (*rel)(char, char)) { int i, j; int size=0; while (s[size] !='\0') { size++; } for (i=size-1; i>0;i--) for (j=0; j<i; j++) if(!rel(s[j],s[j+1])) swap(s,j,j+1); } int main() { char str[1000]; char str0[]="CTEC2901 Data Structure and Algorithms"; strcpy(str, str0); printf("Original: \'%s\'\n",str); bubblesort(str,less_eq); printf("Ascending: \'%s\'\n",str); strcpy(str, str0); bubblesort(str,greater_eq); printf("Descending: \'%s\'\n",str); return 0; }
- 8 replies
-
- c
- passing my reference
-
(and 4 more)
Tagged with:
-
Hi guys My friend gave me an older keyboard (HP) that I really like typing and gaming on. It has quite a few extra buttons (open browser,play/pause music,email, etc.), but only the volume knob and the standby button work. I think it's because it's an older keyboard. There are also no windows 10 drivers for it, but I know there is a program that lets you assign functions/macros to these keys, but I just can't remember what it's called or find it. Do you have any suggestions?
-
So i am trying to make it possible so in one function it will define a variable, but above that i need another function to use that same variable (this is because i am calling that fiunction in the first function). How can I do this? import re def rollDieWithPoints(): print("hi") print(user1) print(user2) print("hi again") def authentication(): file = open("users.txt") text = file.read() user1NameInput = input("What is your name user 1?: ") re_res = re.search(r'^{}, (.*)$'.format(user1NameInput), text, re.MULTILINE) if re_res: password = re_res.group(1) user1PasswordInput = input("What is you password user 1?: ") if password == user1PasswordInput: print("Welcome to the game user 1, now for user 2") user2NameInput = input("What is your name user 2?: ") re_res = re.search(r'^{}, (.*)$'.format(user2NameInput), text, re.MULTILINE) if re_res: password = re_res.group(1) userPasswordInput = input("What is you password user 2?: ") if password == userPasswordInput: print("Welcome to the game, now you can both play") user1 = user1NameInput user2 = user2NameInput rollDieWithPoints() else: print("Wrong password, try again") else: newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ") if newUsernameInput == "Y": file = open("users.txt", "a") newUsernamePasswordInput = input("Please enter your password?: ") file.write("\n" + userNameInput + ", " + newUsernamePasswordInput) file.close() elif newUsernameInput == "N": print("Okay, username incorrect, try again") else: print("That is not an input, please try again") else: print("Wrong password, try again") else: newUsernameInput = input("That username is not in the system, would you like to create a new user by that name? (Y/N): ") if newUsernameInput == "Y": file = open("users.txt", "a") newUsernamePasswordInput = input("Please enter your password?: ") file.write("\n" + userNameInput + ", " + newUsernamePasswordInput) file.close() elif newUsernameInput == "N": print("Okay, username incorrect, try again") else: print("That is not an input, please try again") file.close() def start(): authentication() start() I want the initial definitions to happen in authentication() and then the use to happen in rollDieWithPoints(). Thanks!
-
Hi, I am writing a C program that will take a certain number of characters (a word), print them backwards, and then tell the user whether or not the word is a palindrome. I ran into trouble with the scanf() function when trying to scan user input to a char variable after previously scanning into an int variable. After a quick Google search, I found out the problem was that I was not putting a space before the %c in my scanf function. But even after I put the space, the program would still loop infinitely, and I'm not sure whether it is a problem with my recursive string function or the scanf function I am using. Here is the code: #include<stdio.h> #include<stdlib.h> main() { int getString(); int maxChar; printf("Maximum number of characters for this run? "); scanf("%d", &maxChar); printf("\nEnter no more than %d characters: ", maxChar); getString(); printf("\n"); } int getString() { char c; scanf(" %c", &c); if(c != '\n') { getString(); printf("%c", c); } } As this is a homework assignment for a C class, I am not allowed to use strlen() or any other special functions. I can only use functions that I define myself (besides basic ones like printf() and scanf()). The program I have right now will take a string that the user enters and print it backwards, but only if I get rid of the scanf for the maxChar variable. Here is what the normal output looks like when it is working properly: And here it is with the new scanf function: The program will get hung here, I assume looping infinitely for some reason. If i remove the space in front of the %c in the getString() funciton, the program will crash after the max characters input like so: I assume this is not a crash but rather the program finishing after the scanf function in the getString() function reads the whitespace from the previous scanf and then cancels the if statement and prints nothing, terminating the program. I am not sure how to get around this, as we are not supposed to keep the main function relatively empty and use our own defined functions to scan user input. EDIT: I've been messing around with the program a bit just trying to get an external function which can read user input and then store it in the string array, however I have been getting a segmentation fault when I run the program. Here is the code: #include <stdio.h> main() { int getString(int maxCharacters, char characterArray[100]); int reverse(); int isPalindrome(); int maxCharacters = 0; char characterArray[100]; char programLoop = 'y'; do { printf("Maximum number of characters for this run?\n"); scanf("%d", & maxCharacters); printf("The maximum number of characters is %d\n", maxCharacters); getString(maxCharacters, characterArray); printf("Run the program again? (y/n)\n"); scanf(" %c", &programLoop); } while(programLoop == 'y'); } int getString(int maxCharacters, char characterArray[100]) { printf(" Enter no more than %d characters: ", maxCharacters); scanf(" %s", &characterArray); printf("%s\n", characterArray); } The result looks like this: I feel like its a problem in a way I am passing the characterArray array as an argument for the getString() function, but I'm not sure what I am doing wrong... Any advice would be greatly appreciated, Thanks!
-
#include<stdio.h> #include<malloc.h> void SError(int); void WriteFile(void(*functionPtr)(int));/* Parameter is function pointer */ int BinaryWriteToFile(void); void main(void) { void(*functionPtr)(int); functionPtr = SError; WriteFile(functionPtr); } void SError(int errorNum) { static char*err[] = { "Cannot Open File\n", "Read Error\n", "Write Error\n"}; puts(err[errorNum]); } void WriteFile(void(*errorFunction) (int)) { int error = BinaryWriteToFile(); if(error)errorFunction(error); } int BinaryWriteToFile(void) { int errorCode = 2;// Fictitiousdisk occurred, report for logging to file via SError... return errorCode; } I want to be able to modify the call-back example,by inserting pointer parameter into the arguments lists of the function calls as required,to return the error-message test to main(). Display the error-message text on-screen in main()before terminating the program. But i am really struggling with where to start etc. Can someone point anything out to me?
-
Hi there, I've read upon how Google Sheets uses their servers to do the calculations instead of having your own personalized computer do it. My colleague says he recently found a button within Google Sheets to add Javascript functions and hopefully it would decrease the time/latency needed in order to calculate a specific cell, vlookup/hlookup from sheets to sheets and also refresh the sheets more quickly. My question is, would Javascript functions actually decrease the amount of time needed for Google's servers to calculate all the stuff on Sheets? FYI these sheets are for company use so there's a lot of stuff on each sheet and that there are more than 5 sheets. I've also found this one button specifically to change the refresh times into "Google Finance" but if the sheets had a formula that contained "Importrange", it would be different I think. My idea to him however is that we'd need better hardware in order to receive information quicker. Our internet is "decent" imo and I think we're lacking in hardware speeds. That being said though, it doesn't have much affect between personalized computers and Google's Servers I would hope to assume.
-
Hey guys, I am currently stuck at what I should be having as my function arguments. I don't know if it should have head/rear and whether or not they should be passed by reference or value. Also, I am very iffy on how I would have my program display if a match is found becauseI am not allowed to display anything from my functions and it is supposed to be taken care of by main. Any tips are appreciated. My main currently has almost nothing Here are my .cpp and .h files: funcions.cpp header.h main.cpp
-
Right I need to write a program that finds the first N of factorial numbers, much like this but i need to write a function using recursion to do so. So far I've got it working to find the factorial of one number, but i can't seem to get it too display all the first factorial numbers up to N. Here is the code: #include <stdio.h> int fact (int x) { if (x == 1) return 1; else return x * fact(x-1); } int main() { int n, i; printf("Enter an intger number: "); scanf("%i", &n); printf("\n"); for (i = 0; i<n; i++) { printf("Factorial of %i is %i\n", i+1, fact(n)); } } And here is the output: As you can see it displays the factorial of the N only , not all the factorials up to N, as well.
- 11 replies
-
- first n factroial
- recursion
-
(and 3 more)
Tagged with:
-
Right so I have these set of exercises and i'm stuck on the 3rd exercise. Which wants me to write a function for strisalpha. The function is supposed to return 1 if the string contains only alphabetic characters else returns 0. Here is the main function code for that function: case 3 : testing("strisalpha"); promptAndGetString(stringA); if (strisalpha(stringA)) printf(" \"%s\" contains only alphabetic chars\n",stringA, strisalpha); else printf(" \"%s\" contains non-alphabetic chars\n",stringA, strisalpha); if (testno != 1) break; And here is the complement function with the code i've written so far. int strisalpha(char * string) { /* returns 1 if string contains only alphabetic characters else returns 0 */ char i = 0; if (string[i] >= 'a' && string[i] <= 'z') return 1; else return 0; } For some reason the return statements in the function are not doing anything when I run the program, (see the attatched image) If anyone could help me sort this i'd appreciate it. Also if it helps here is the entire program for all the exercises: #include <stdio.h> /*----------------------------------------------------------------------------------*/ int strlength(char * string) { /* returns the length of a string */ /* We have provided the answer to this first exercise to get you started: */ int i=0; while (string[i]) i++; return i; } /*----------------------------------------------------------------------------------*/ int strisalpha(char * string) { /* returns 1 if string contains only alphabetic characters else returns 0 */ char i = 0; if (string[i] >= 'a' && string[i] <= 'z') return 1; else return 0; } /*----------------------------------------------------------------------------------*/ int strfirst(char * string, char letter) { /* returns the index of the first occurrence of letter in string or -1 if it does not occur */ } /*----------------------------------------------------------------------------------*/ int strlast(char * string, char letter) { /* returns the index of the last occurence of letter in string or -1 if it does not occur */ } /*----------------------------------------------------------------------------------*/ void strcopy(char * string1, char * string2) { /* copies string1 to string 2 */ } /*----------------------------------------------------------------------------------*/ void strhead(char * string1, char * string2, int n) { /* copies the first n characters of string1 to string2 */ } /*----------------------------------------------------------------------------------*/ void strtail(char * string1, char * string2, int n) { /* copies the last n characters of string1 to string2 */ } /*----------------------------------------------------------------------------------*/ void strmid(char * string1, int start, int end, char * string2) { /* copies the characters of string1 from start to end (inclusive) to string2 */ } /*----------------------------------------------------------------------------------*/ void strnodup(char *string) { /* removes any duplicate characters from string1 e.g. "abcdabcaba" would become "abcd" */ } /*----------------------------------------------------------------------------------*/ int isprefix(char * string1, char * string2) { /* if string1 is a prefix of string2 returns 1 else returns 0 e.g. isprefix("hello","hello world") returns 1 isprefix("world","hello world") returns 0 Note that we define an empty string to be a valid prefix of any string. Thus isprefix("", "hello world") returns 1 and isprefix("", "") returns 1 */ } /*----------------------------------------------------------------------------------*/ /* If you choose to answer coursework option A then You will need to put the function that answers courswork Option A here */ /*----------------------------------------------------------------------------------*/ /* If you choose to answer coursework option B then You will need to put the function that answers courswork Option B here */ /*----------------------------------------------------------------------------------*/ /* Utility functions */ /*----------------------------------------------------------------------------------*/ int promptUserForChoice() { int choice = -1; char input[256]; while (choice < 0 || choice > 13) { printf("\n"); printf("--------------------------------------------------------\n"); printf("Options:\n"); printf(" QUIT TESTING (0) TEST strhead (7) \n"); printf(" TEST EVERYTHING (1) TEST strtail (8) \n"); printf(" TEST strlength (2) TEST strmid (9) \n"); printf(" TEST strisalpha (3) TEST strnodup (10) \n"); printf(" TEST strfirst (4) TEST isprefix (11) \n"); printf(" TEST strlast (5) CWK question A (12) \n"); printf(" TEST strcopy (6) CWK question B (13) \n"); printf("Choice ? "); scanf(" %s", input); choice = atoi(input); printf("Selected %i\n\n", choice); } return choice; } void testing(char * name) { printf("\n"); printf("--------------------------------------------------------\n"); printf(" Testing:\t%s\n",name); printf("\n"); } void promptAndGetString(char * str) { char junk; str[0] = '\0'; printf("Enter a string : "); scanf("%c%[^\n]", &junk, str); printf("You entered \"%s\"\n",str); } /*----------------------------------------------------------------------------------*/ /* Main Program */ /*----------------------------------------------------------------------------------*/ main() { char stringA[1024], stringB[1024]; int start, end, n, i, testno; char letter; testno = promptUserForChoice(); while (testno != 0) { switch (testno) { case 1 : printf(">> \tSelected everything\n"); printf(">> \tEach test will be run once\n"); case 2 : testing("strlength"); promptAndGetString(stringA); n=strlength(stringA); printf("\"%s\" has length %d\n",stringA,n); if (testno != 1) break; case 3 : testing("strisalpha"); promptAndGetString(stringA); if (strisalpha(stringA)) printf(" \"%s\" contains only alphabetic chars\n",stringA, strisalpha); else printf(" \"%s\" contains non-alphabetic chars\n",stringA, strisalpha); if (testno != 1) break; case 4 : testing("strfirst"); promptAndGetString(stringA); printf("Input a letter (char)? "); scanf(" %c", &letter); n=strfirst(stringA,letter); if (n<0) printf(" \"%s\" does not contain %c\n",stringA, letter) ; else printf(" %c first occurs as \"%s\"[%d] \n",letter, stringA, n) ; if (testno != 1) break; case 5 : testing("strlast"); promptAndGetString(stringA); printf("Input a letter (char)? "); scanf(" %c", &letter); n=strlast(stringA,letter); if (n<0) printf(" \"%s\" does not contain %c\n",stringA, letter) ; else printf(" %c last occurs as \"%s\"[%d] \n",letter, stringA, n) ; if (testno != 1) break; case 6 : testing("strcopy"); promptAndGetString(stringA); strcopy(stringA,stringB); printf("Original string is\t\"%s\"\n",stringA); printf("..Copied string is\t\"%s\"\n",stringB); if (testno != 1) break; case 7 : testing("strhead"); promptAndGetString(stringA); printf("Take how many from head? "); scanf(" %i",&n); strhead(stringA,stringB,n); printf(" First %d of \"%s\" is \"%s\"\n",n,stringA,stringB); if (testno != 1) break; case 8 : testing("strtail"); promptAndGetString(stringA); printf("Take how many from tail? "); scanf(" %i",&n); strtail(stringA,stringB,n); printf(" Last %d of \"%s\" is \"%s\"\n",n,stringA,stringB); if (testno != 1) break; case 9 : testing("strmid"); promptAndGetString(stringA); printf("Start from which position? "); scanf(" %i",&start); printf("Up to which position? "); scanf(" %i",&end); strmid(stringA,start,end,stringB); printf(" \"%s\" [%d .. %d] is \"%s\"\n",stringA,start,end,stringB); if (testno != 1) break; case 10 : testing("strnodup"); promptAndGetString(stringA); strnodup(stringA); printf(" with duplicates removed is \"%s\"\n",stringA); if (testno != 1) break; case 11 : testing("isprefix"); printf("string1: "); promptAndGetString(stringA); printf("string2: "); promptAndGetString(stringB); n=isprefix(stringA,stringB); if (n) printf(" \"%s\" is a prefix of %s\n",stringA, stringB); else printf(" \"%s\" is NOT a prefix of %s\n",stringA, stringB); if (testno != 1) break; case 12 : testing("Coursework Option A"); printf("During the coursework part 2 practical test you may\n"); printf("choose to implement this option (A). It will be easier\n"); printf("to implement than option B and, as such, will not gain\n"); printf("maximum marks - even for a perfect solution. Therefore,\n"); printf("the penalties and rewards are lower for this option.\n"); printf("The specification of this option (A) wil be published\n"); printf("on the day of the test itself.\n"); if (testno != 1) break; case 13 : testing("Coursework Option B"); printf("During the coursework part 2 practical test you may\n"); printf("choose to implement this option (B). It will be more\n"); printf("difficult to implement than option A and, as such, will\n"); printf("permit maximum marks to be gained. However, a poor\n"); printf("solution will be marked rigorously. So the penalties\n"); printf("and rewards are higher for this option.\n"); printf("The specification of this option (B) wil be published\n"); printf("on the day of the test itself.\n"); if (testno != 1) break; default : printf("SWITCH ERROR: Should never get here!\n"); } /* end switch */ testno = promptUserForChoice(); } /* end while */ printf("\nBYE :-)\n"); } /* end main */
- 13 replies
-
- programming
- c
-
(and 2 more)
Tagged with:
-
Okay, i'm really in the gutter this year with math... I have an F for the first semester, and this being the last year of high school, i need to wake up... I blame the teacher for this, since she couldn't explain to me for half a year that f(x)=y (is one way to write it).... Yup, her way of teaching is VERY incompatible with me, but i need a D... This is what we're doing, and we'll be doing for the next 4 months (after that comes a part that has something to do with percentages and economy, but that's my forte and i'm the best in the class when it comes to it, so i'm not worried), so does anyone know a site where this stuff is nicely explained, not just throwing a huge number of problems down your throat.... Thanks in advance for help
-
Hello everyone im in the process off making a spreadsheet which is part of my first assignment in university (UK) and i have got a few questions that i was wondering if you guys would have any advice trying to solve, will do my best to explain them and will attach the files if i can 1) i have this set of dates that each fall between a certain season, i have created a function so that when you type in a date it will return what season its in but dont think its very efficient, has anyone got a better soluction to this. my function i wrote is under the photo function: =IF(AND(C18>=C13,C18<=D13),C12,IF(AND(C18>=C14,C18<=D14),C12,IF(AND(C18>=C15,C18<=D15),C12,IF(AND(C18>=E13,C18<=F13),E12,IF(AND(C18>=E14,C18<=F14),E12,IF(AND(C18>=E15,C18<=F15),E12,IF(AND(C18>=G13,C18<=H13),G12,IF(AND(C18>=G14,C18<=H14),G12,IF(AND(C18>=G15,C18<=H15),G12,"ERROR"))))))))) i then need to work out how to use the season information for each pricing as it changes depending, any help would be great. cheers
-
- spreadsheets
- lookups
-
(and 1 more)
Tagged with:
-
foreach function without argument? Would it work? I already tried it and it worked! LOL but I'm kinda confuse why? So, I have a stdClass object which can be extracted using foreach right? for example: Array( [0] => stdClass Object ( [username] => derp [password] => derppass [email] => derpmail )) can be extracted like this: (assuming the array name is $array) foreach($array as $arr){ echo $arr->username . '<br>'; echo $arr->password. '<br>'; echo $arr->email. '<br>';} But what I did is I didn't put any argument in foreach and called it outside the function like this: foreach($array as $arr){ //empty lol}echo $arr->username;echo $arr->password;echo $arr->email; and it still worked! Dont have any error, but i think thats because my error_reporting setting is set to production value.... Is this safe to use?