This semester is my first programming class and I am really struggling to complete this project. I just can't get a handle on this.I know its simple but my program is not running. Can someone help me and explain it ?

Here are the rules(outline):

  • You must ask the user to enter the initial value (Hint: into an array of chars)
  • You must be able to handle numbers at least up to 20 digits long. Notice that this is bigger than an integer variable can hold (hence the need for arrays)
  • You must use arrays to manipulate the digits (Hint: you know how to reverse arrays)

i don't really know what what to do after this.

My Code:

#include <stdio.h> 
int main() 
{ 
//2.You must be able to handle numbers at least up to 20 digits long. Notice that this is bigger than an integer variable can hold (hence the need for arrays) 

char a[21] = ""; //Define an empty string  
char val; 

//1.You must ask the user to enter the initial value (Hint: into an array of chars) 

printf("Please enter a value for the element: "); 
scanf("%d", &val); 

return 0; 
} 

I have a question about this line

scanf("%d", &val);

Why are you reading a integer into a character? Won't it make more sense to read the characters directly into the character array?

scanf("%20s", a); 

Hi,

I think you will do yourself a lot of good if you really understand data types in C. So that you don't confused char data type with integer data type. Then also note that in C, string is just char array. So understanding array is key.

gerard4143 raised a very valid point as to using scanf("%20s", a);. However, if your array of character has a space scanf wouldn't get the rest of your value or input, even when is less than 20 character long.

So, either use fgets like so fgets(str, sizeof(str), stdin); or use scanf like thus scanf("%20[^\n]", str);

Another important thing you should also take note of is the fact that array start with the index 0 NOT 1. So with char a[21] you have more than! Even with index placed at 20, because counting from 0 to 19 should give 20 character long.. :)

Edited 3 Years Ago by 2teez

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
int i=0,k=0;
int tmp1=0, tmp2=0, tmp3=0;
char * a;
a= (char *) malloc(30 * sizeof(char));

char * b;
b= (char *) malloc(30 * sizeof(char));

int * x;
x= (int *) malloc(30 * sizeof(int));

int * y;
y= (int *) malloc(30 * sizeof(int));

int * z;
z= (int *) malloc(61 * sizeof(int));

int * w;
w= (int *) malloc(31 * sizeof(int));

int * v;
v= (int *) malloc(61 * sizeof(int));

int * solution;
solution= (int *) malloc(61 * sizeof(int));


int carry = 0;
int c[61];
int d[61];

int f[61];
int g[61];

printf("\nEnter a number (up to 30 digits in length)> "); scanf("%s", a);

i=0;
while(i < strlen(a)){
c[i] = a[i] - '0';
i++;}
printf("\nThe first number is: ");
for(i=0; i < strlen(a); i++){
printf("%d", c[i]);}
printf("\n");

for(i=0, k = 29; i < strlen(a); i++){
x[k] = c[(strlen(a)-1)-i];
k--;}

printf("\nEnter a second number (up to 30 digits in length)> "); scanf("%s", b);

i=0;
while(i < strlen(b)){
d[i] = b[i] - '0';
i++;}

printf("\nThe second number is: ");
for(i=0; i < strlen(b); i++){
printf("%d", d[i]);}
printf("\n");

for(i=0, k = 29; i < strlen(b); i++){
y[k] = d[(strlen(b)-1)-i];
printf("\nThe sum of the two numbers is: ");

for(k=30; k>=0; k--)
{ 
w[k] = 0; 
}

for (k = 29; k >= 0; k--){
w[k] = x[k] + y[k] + w[k];
if (w[k] >= 10){
w[k] = w[k]%10;
w[k-1] = 1;}
}
for(i= -1; i < 30; i++){
printf("%d", w[i]);}
printf("\n");

for(k = 60; k>=0; k--){
z[k] = 0;
f[k] = 0;
g[k] = 0;
solution[k] = 0;
}
for(k = 0; k < 30; k++){
f[k+31] = x[k];
g[k+31] = y[k];
}

printf("\nThe product of the two numbers is: ");

/*The multiplication algorithm begins here*/

for(i= 60; i >= 0; i--) {
for(k=60; k>=0; k--)
{ tmp1 = f[i] * g[k];
z[k] = tmp3 + z[k-1];
tmp3 = tmp1 % 10;
z[k-1]= tmp1 / 10;
}
}

/*The multiplication algorithm ends here*/

for(i = 0; i < 61; i++){
printf("%d", z[i]);}
printf("\n\n");

return(0);
}

i guess this gonna help you... incase if you encounter any problem in understanding any piece of code kindly let me know i will explain..

#include<stdio.h>
int main()


int array[];    // answer to number 2. "that declarion is dynamic array, it can hold many digit"

printf("Please enter the initial value");   // can you please elaborate more the first question,, value of what? 
scanf("%d", &variable_name);    // for integer
scanf("%c", &variable_name);    // for character


    // in number 3, there are may ways to manipulate the array., but as you indicated, you would like to reverse it right? you can use "for loop"for that.

}

Im fully not understand what really the output you want to get, because in programming as my professor always said that in order to create a program you must know first the output before you create a code ...

Edited 2 Years Ago by viovicente

as my professor always said that in order to create a program you must know first the output before you create a code ...

Yes, I would agree with your professor. How can you write a program to do something if you don't know what the program is supposed to do? You can't bake a pie if you don't know what kind of pie you want.

a= (char *) malloc(30 * sizeof(char));

In C language it is not necessary to typecase the return value of malloc() or any other function that returns void* value. But --- follow your instructors requirement for that becaue typecasting the return value from malloc() is optional.
a= malloc(30 * sizeof(char));

Another problem with the above is that sizeof(char) is not needed because sizeof(char) is guarenteed to always be 1.

a= malloc(30);

With such small amount of memory I question the reason for using malloc at all -- why not just make the array static?
char a[30];

Edited 2 Years Ago by Ancient Dragon

This article has been dead for over six months. Start a new discussion instead.