C :: Functions Retuning Non Integers
Apr 5, 2013
from k&r book (4.2 - functions retuning non integers)
(doesnt compile - 1 error)
Code:
#include <stdio.h>
#define MAXLINE 100
/* rudimentary calculator */
main()
{
double sum, atof(char []);
char line[MAXLINE];
int getline(char line[], int max);
sum = 0;
while (getline(line, MAXLINE) > 0)
printf(" %g
", sum += atof(line));
return 0;
}
Code:
int getline(char line[], int max); is no different from:
Code:
getline(char line[], int max); ?
so, the int (return type) is optional when using a function. i assume return type is casted double? why the (char [])? just weird cuz i never seen it before
View 2 Replies
ADVERTISEMENT
Nov 12, 2013
I am trying to create a program that reads my file filled with random words, it then compares the words after they are put into a 2d array and sees if there is any matching words.. unfortunately the count is not working for me (in function2 and function3) and I am not sure why..
Code:
#include<stdio.h>
#include<string.h>
char function1(char words_array[][17]);
int function2(char words_array[][17]);
void function3(int pairs, char words_array[][17]);
int main( void )
{ char words_array[20][17];
int x = 0;
[Code]...
View 6 Replies
View Related
Mar 29, 2014
current functions are:
char *createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(char *environment, int width, int height);
need to be:
int* createEnvironment(int width, int height, int numWoodChips);
void printEvrionment(int* environment, int width, int height);
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
[Code]....
View 1 Replies
View Related
Nov 7, 2014
I am working on a number of utility functions for two dimensional arrays of integers, or matrices. However I am having a problem with segmentation faults, most likely due to errors in using malloc in functions like copyMatrix.
Code:
matrix_utils.h~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//This function checks if two matrices are equal
int isEqual(int **A, int **B, int n);
//This function returns one row of a matrix
int * getRow(int **A, int i, int n);
//This function returns one column of a matrix
int * getCol(int **A, int j, int n);
[Code] ....
View 2 Replies
View Related
Apr 21, 2014
I'm a beginner at c++ and I need to write a program that reads a set of integers and then finds and prints the sum of the even and odd integers. The program cannot tell the user how many integers to enter. I need to have separate totals for the even and odd numbers. what would I need to use so that I can read whatever number of values the user inputs and get the sum of even and odd?
View 2 Replies
View Related
Mar 20, 2014
I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.
We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.
As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.
NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.
Stack.h
#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;
[Code]...
View 3 Replies
View Related
May 3, 2013
At the moment im trying out with pointing to an array of functions. I got this working as following:
typedef void (* functionPtr) ();
functionPtr functions[2][2]={{do11,do12}, {do21,do22}};
void do11(){DEBUG_PRINTLN("11");}
void do12(){DEBUG_PRINTLN("12");}
void do21(){DEBUG_PRINTLN("21");}
void do22(){DEBUG_PRINTLN("22");}
void loop(){
A=0;
B=1;
functions[A][b]();
}
But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:
error: argument of type 'void (Basic::)()' does not match 'void (*)()'
View 2 Replies
View Related
Oct 5, 2014
Write a program, sumit.c, that reads an integer value n from the user and then sums the integers between 0 and n. Your program should be able to handle both positive and negative values of n and should write the sum to the output file sumitout.txt. Note that your sum should not actually include 0, since that doesn't affect the sum. Your program should open the file sumitout.txt for appending so that the file can record a series of runs.
For marking purposes run your program twice with values n = −10 and n = 5.
Here is a sample run:
Enter n: -4 The sum of integers from -1 to -4 is -1
#include<stdio.h>
int main(void) {
//declare your variables
int n, i, term, total = 0;
//promt user to enter and read a value of n
printf("Enter n: ");
scanf("%d", &n);
//calculate the sum
[Code] ....
View 2 Replies
View Related
May 14, 2014
i am relatively new to C programming so i run into problems on daily basis. But this time i have something i just cant figuer out and i was hoping you could point me towards the right track. I am trying to divide two integers.DevValue by KpTotal. for some reason my micro controller allways crashes.Y is a variable of a distance measuring sensor. i have a 4x4 keypad to enter a three digit number (e.i 123) so Kp1 = 1 Kp2 = 2 Kp3 =3.
Code:
int kp1, kp2, kp3, kpTotal = 0;
char txt[6] = ""
int keypadPort at PORTD;
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
}
[code]....
i think it has something to do with the format of the value. i read that the micro controller crash when dividing by zero.
View 1 Replies
View Related
Mar 12, 2014
How to write a simple function that will take 3 ints and find the sum of the higher 2? This is what i got so far:
int findsum(int a,int b,int c)// will find the highest int and return it to our main program {
int max,max2;// this sets our local variable max
// next we will find the larger of our first 2 variables
if( a>=b)
{ max=a;
[Code] ....
How to get the second highest number and add it to max
View 19 Replies
View Related
Apr 18, 2014
I am working on an assignment where I have to subtract two very large integers. How can I write it's code provided that I have both numbers in a character array with each index holding a fraction of the original number. Like if I have a number 123456789 in an array then
arr[0]='1';
arr[1]='2';
arr[2]='3';
arr[3]='4';
and so on. n
nNw if i have to subtract this number from an other number, how can I do that?
View 4 Replies
View Related
Nov 8, 2014
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
[Code] ......
How do I link the strings to the integers?
View 4 Replies
View Related
Oct 17, 2014
I'm trying to make a C++ program that generate 6 random numbers ( from 100,000 to 999,999 ), okay, so I wrote a few lines..
Code:
srand(time(0));
for (int i = 0; i < 5; i++)
{
std::cout << 100000 + (rand() % 999999) << std::endl;
}
The problem is, that it generates numbers like this:
117,207
123,303
131,083
... etc etc..
They're all starts with 100K, i want them to be an actual random..
View 8 Replies
View Related
Dec 3, 2013
I am doing a program and i need to read integers that are in a string and i dont know if i am doing it correctly.
Code:
unsigned int j=0, h1;
char num[100] , str1[100], str2[100], a[100], b[100];
sscanf(str1, "%[^;] ; %[^;] ; %d ", a, b, &h1); the string is : "0048915433190;24/10/2013;13h 7m 47s;13h 8m 59s;0;1;"
And what i want to read with sscanf is the hours, minutes and seconds.
View 1 Replies
View Related
Oct 15, 2014
How i can concatenate two integers into one integer. I have code that concatenate two integers but if the 2nd integer is zero it won't work. How can i modify it so that it can cater the case of y=0 too.
Code:
int concatenate(int x, int y) {
int pow = 10;
while(y >= pow)
pow *= 10;
return x * pow + y;
}
View 12 Replies
View Related
Dec 2, 2014
I compiled a simple program using a function to add two integers, in order to understand the difference of calling by value vs by reference.
Code:
#include <stdio.h>
int add(int a, int b, int sum);
int main()
{
int a, b, sum;
printf("Please enter two integers
");
scanf("%d%d", &a, &b);
[Code]...
I used these two (one, two) pages I found in the net while studying the subject. So here are my questions...
(a) Do I understand correctly that in the above code all a, b and sum are passed by value?
(b) If I wanted only sum to be passed by reference, how the above code would change?
(c) If I wanted all (a, b, sum) to be passed by reference, how the above code would change?
View 12 Replies
View Related
Jan 22, 2013
"Write a program that prompts the user for an integer number from the keyboard, and store it in a variable num. After each number is typed in, add num to a total.
Do this six times, each time adding num to a total. (Be sure to reuse the same variable named num for each of the six numbers entered.) When all six numbers have been entered from the keyboard, calculate an average. Display the total and average calculated. "
Here is what I have so far:
Code:
#include<stdio.h>
int main() {
int num, total1, total2, total3, total4, total5, total6, avg;
printf("Enter first number:");
scanf("%2d",&num);
[Code] .....
View 2 Replies
View Related
Mar 25, 2013
I am trying to make a game where you have a secret code that is coded with colors like ROYG (red,orange,yellow,green) and I am having trouble when it tells you when you have a right color in the right spot or a right color in the wrong spot when you guess a color. How can I change my code under the function int comparearray where it will compare pointers to pointers and not integers and give me the correct number of "almost" and "correct".
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ROWS 100
#define COLS 4
}
[code]....
View 4 Replies
View Related
Feb 25, 2014
How do I do the operation of two integers that gives you the results?
I'm supposed to write a program that:
Asks the user for an integer
Asks the user for one of '+', '-', '*', or '/' (as a character)
Asks the user for another integer
Performs the given operation on the two integers, and prints out the result of
Please enter an integer: 4
Please enter an operation (+, - , *, /): *
Please enter another integer: 5
4 * 5 = 20
Code:
#include <iostream>
using namespace std;
int main() {
int x;
int c;
int d;
char e;
[Code] ....
View 7 Replies
View Related
Apr 5, 2013
I have a file where the first column shows letters, second column shows numbers.
How could I make it to print everything exactly how it is in the file – a vector/array with strings and integers?
View 13 Replies
View Related
Oct 2, 2014
I took a month's break from programming, and got rusty. Here's the problem:
The character 'b' is char('a'+1), 'c' is char('a'+2), etc. Use a loop to write out a table of characters with their corresponding integer values:
a (tab) 97
b (tab) 98
...
z (tab) 122
View 2 Replies
View Related
Jan 26, 2014
I'm confused about the actual value of 8 bytes for unsigned integers.
The below code suggests the value is 13217906525252912201:
#include <stdio.h>
#include <inttypes.h>
typedef uint64_t byte_int_t;
int main(void){
byte_int_t t;
printf("%" PRIu64 "
", t);
}
./runprogram
13217906525252912201
However, when I use a calculator, I get a different value: 2^64= 1.8446744e+19
So I was wondering is this really 8 bytes? So I try below test and it produces 8, as expected:
#include <stdio.h>
#include <inttypes.h>
typedef uint64_t byte_int_t;
int main(void) {
byte_int_t t;
printf("%u
", sizeof(t));
return 0;
}
So why does C and my calculator provide two different results?
View 1 Replies
View Related
Apr 18, 2013
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
[code]....
I do not know how to output on the screen, I believe that i have written it to the file correctly but i dont know how to output it.
View 1 Replies
View Related
Jan 29, 2015
I have been tasked with sorting a text file with some numbers in it. For example, there can be 5 numbers in it: 1,2,3, 4, and 5. However, they are out of order (3,2,4,1 and 5). I need the numbers in numerical order. How can you sort the numbers into a new file?
Bubblesorting? I can not use arrays in this program. I have already determined the minimum number in the file.
View 5 Replies
View Related
Jan 29, 2013
I'm trying to write an algorithm for a larger project that will take two strings which are both large integers (only using 10 digit numbers for the sake of this demo) and add them together to produce a final string that accurately represents the sum of the two original strings. I realize there are potentially better ways to have gone about this from the beginning but I am supposed to specifically use strings of large integers as opposed to a long integer.
My thinking was to take the two original strings, reverse them so their ones position, tens position, and so on all line up properly for adding. Then one position at a time, convert the characters from the strings to single integers and add them together and then use that sum as the ones position or otherwise for the final string, which once completed will also be reversed back to the correct order of characters.
Where I'm running into trouble I think is in preparing for the event in which the two integers from the corresponding positions in their strings add to a sum greater than 9, and I would then have carry over some remainder to the next position. For example, if I had 7 and 5 in my ones positions that would add to 12, so I would keep the 2 and add 1 to the tens position once it looped back around for the tens position operation.
I'm not getting results that are in any way accurate and after spending a large amount of time stumbling over myself trying to rectify my algorithm, I am not sure what I need to do to fix this.
#include <iostream>
#include <cstdlib>
#include <string>
[Code]....
View 6 Replies
View Related
Feb 11, 2013
I have 2 integers, both in 2 seperate classes in 2 seperate files. I have main.cpp, test_01.h, Test_01.cpp, Player.h and Player.cpp. I ahve a function which takes an integer (a) as a parameter (from Test_01.h and Test_01.cpp). a sets another integer (attackPower) from the same class equal to a. The integer (health) in the other class (Player.h and Player.cpp), is then equal to health -= attackPower. But instead of giving me the right answer 75 (health = 100 and a is set to 25 when i call the function) it gives me the answer.
main.cpp
#include <iostream>
#include "Test_01.h"
#include "Player.h"
using namespace std;
int main() {
Player p;
Test_01 t;
[Code] ....
View 2 Replies
View Related